【问题标题】:Running a elevated cmd prompt command python运行提升的 cmd 提示符命令 python
【发布时间】:2020-03-08 13:34:53
【问题描述】:

我已经搜索了谷歌等,但正在努力寻找合适的答案......

在 python 中打开提升的 cmd 提示并让 python 在其中运行命令的最简单方法是什么?我不介意将代码中的用户名和密码指定为仅用于测试

到目前为止,我想出了这个:

subprocess.run(["Enable_IPRouting.bat"])

“Enable_IPRouting.bat”至少包含以下内容:

runas /profile /user:DESKTOP-GRLIH2J\Administrator cmd

现在,当我运行它时 - 它似乎可以工作,我收到一个要求输入管理员密码的提示,并出现一个 cmd 提示。 但是,当我尝试执行需要管理员权限的 cmd 时,我得到了通常的错误: The requested operation requires elevation

我错过了什么?

谢谢

【问题讨论】:

  • RunAs 不会提升它只是作为用户运行。
  • 谢谢 - 那么有没有办法提升它?
  • 右键单击并选择以管理员身份运行或编写一个exe并将清单放入其中。这显示了如何使用清单 winsourcecode.blogspot.com/2019/12/… 编写控制台 exe。
  • 如果您复制您的 python 程序并使用angusj.com/resourcehacker 编辑它的清单,那么您可以选择提升或不提升的 python。

标签: python windows cmd


【解决方案1】:

你有没有试过例如

os.system('runas /profile /user:DESKTOP-GRLIH2J\Administrator enable_iprouting.cmd')

(如果enable_iprouting.cmd 包含要运行提升的东西)?

【讨论】:

  • 我刚刚尝试过,我在 VSCode 终端中得到了一个 Attempting to start enable_iprouting2.bat as user "DESKTOP-GRLIH2J\Administrator" - 但是什么也没发生。
猜你喜欢
  • 2012-05-02
  • 1970-01-01
  • 2010-11-30
  • 1970-01-01
  • 2023-03-25
  • 2012-10-03
  • 1970-01-01
相关资源
最近更新 更多