Set administrator privileges to subprocess.check_call() in Python

后端 未结 2 618
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 08:21

I call two executables from a python script one which needs Administrator privileges and one which does not need them. Is there any way by which I can set the Administrator

相关标签:
2条回答
  • 2020-12-17 08:37

    I found a nasty workaround

        f = open('test.cmd', 'w+')
        f.write("execute.exe")
        f.close()
    
        os.system("runas /savecred /profile /user:Administrator \"test.cmd\"")
    

    or you can use subprocess

    0 讨论(0)
  • 2020-12-17 08:37

    Try this out for entering an administrative password.

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

    Here are the docs on:

    Popen - http://docs.python.org/2/library/subprocess.html#popen-constructor

    runas - http://technet.microsoft.com/en-us/library/cc771525.aspx

    if this works will depend on the program NeedsAdminPrivilege.

    0 讨论(0)
提交回复
热议问题