Anonymus
Anonymus

Reputation: 61

Starting Elevated Subprocess In Elevated Python Without Shell

I'm attempting to run an executable that requires elevated privileges using the subprocess library with python. The behaviour works as expected when using shell=true, or first calling powershell.exe with Popen, however when the shell is false(the ideal behaviour since I wish to kill the process later), then I run into OSError: [WinError 740] The requested operation requires elevation. From my understanding the subprocess has elevated privileges. An alternative solution would be a way to kill the shell process.

import ctypes
import subprocess

print(ctypes.windll.shell32.IsUserAnAdmin()) # returns true

command = r'"C:\Users\First Last\HWiNFO64.exe"'
proc = subprocess.Popen(command, shell=False)

Crashes with this

  File subproc.py", line 20, in <module>
    proc = subprocess.Popen(command, shell=False)
  File "C:\Program Files\Python310\lib\subprocess.py", line 966, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Program Files\Python310\lib\subprocess.py", line 1435, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 740] The requested operation requires elevation

Upvotes: 1

Views: 38

Answers (0)

Related Questions