Run process as admin with subprocess.run in python

后端 未结 1 1155
南笙
南笙 2020-12-06 19:26

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 ans

相关标签:
1条回答
  • 2020-12-06 20:15

    since OS is not specified , i will take the example of windows OS

    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>"
    

    for further reference https://technet.microsoft.com/en-us/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()
    
    0 讨论(0)
提交回复
热议问题