在python中使用subprocess.run以管理员身份运行进程 [英] Run process as admin with subprocess.run in python
本文介绍了在python中使用subprocess.run以管理员身份运行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以通过python将 runas = True
arg传递给 subprocess.run
函数?我想以管理员身份运行一个进程(提升它)。感谢您的回答:)\
Is there a way of passing some runas=True
arg to a subprocess.run
function in python? I want to run a process as admin (elevate it). Thanks for answers :)\
编辑:使用Windows操作系统。
Using Windows OS.
推荐答案
由于未指定操作系统,我将以Windows操作系统为例
since OS is not specified , i will take the example of windows OS
窗口具有命令行实用程序 Run as,可以用作
window has a command line utility "Run as" , which can be used as
runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user:<UserAccountName> "<ProgramName> <PathToProgramFile>"
以供进一步参考
https://technet.microsoft.com/zh-CN/library/cc771525.aspx
您可以在下面的代码中使用
You can use in code like below
import subprocess as Popen
import subprocess as sp
prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
prog.stdin.write('password')
prog.communicate()
这篇关于在python中使用subprocess.run以管理员身份运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文