Set administrator privileges to subprocess.check_call() in Python

后端 未结 2 619
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题