【问题标题】:Run process as admin with subprocess.run in python在 python 中使用 subprocess.run 以管理员身份运行进程
【发布时间】:2018-05-02 23:38:56
【问题描述】:

有没有办法将一些runas=True arg 传递给python 中的subprocess.run 函数?我想以管理员身份运行一个进程(提升它)。感谢您的回答:)\

编辑:使用 Windows 操作系统。

【问题讨论】:

  • 操作系统在这里很重要。

标签: python subprocess admin


【解决方案1】:

由于没有指定操作系统,我以MS Windows操作系统为例

Windows有一个命令行实用程序“Run as”,可以作为

  runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user:<UserAccountName> "<ProgramName> <PathToProgramFile>"

供进一步参考 https://technet.microsoft.com/en-us/library/cc771525.aspx

您可以在下面的代码中使用它

import subprocess as sp

prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
prog.stdin.write('password')
prog.communicate()

【讨论】:

    【解决方案2】:

    如果您想以同一用户但具有管理员权限运行命令

    请参考这个解决方案:

    os.system(r'''
    Powershell -Command "& { Start-Process \"notepad.exe\"
     -ArgumentList @(\"C:\\Windows\\System32\\drivers\\etc\\hosts\")
     -Verb RunAs } " '''
    

    原始答案可以在这里找到https://superuser.com/a/753600/1088510

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 2013-05-31
      • 1970-01-01
      • 2014-04-26
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      相关资源
      最近更新 更多