It's a little roundabout, but another way is to run a shell command, launch Powershell (comes with Windows), then tell Powershell to run the .exe
as Admin:
(just remember that the shell command is in CMD, so you escape with backslash, not Powershell's backtick.)
Powershell command:
Start-Process "executable.exe" -ArgumentList @("Arg1", "Arg2") -Verb RunAs
CMD running Powershell:
Powershell -Command "& { Start-Process \"executable.exe\" ... }"
Python running CMD runnning Powershell:
os.system(r'''
Powershell -Command "& { Start-Process \"notepad.exe\"
-ArgumentList @(\"C:\\Windows\\System32\\drivers\\etc\\hosts\")
-Verb RunAs } " '''
Why would you remove the password? – Diblo Dk – 2013-07-04T18:25:30.813
You could use an
stdin
PIPE to send it, but then you need to store it somehow accessible to your Python script. That looks likeproc = subprocess.call(['runas','/user:Administrator','myfile.exe'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Then you could doproc.stdin.write('password\r\n')
. – nerdwaller – 2013-07-04T18:28:04.440@nerdwaller, I'm trying this for a while but without success. Are you sure this works? – McLeary – 2014-02-11T00:30:36.587