在已提权Python进程中不通过Shell启动提权子进程失败的问题求助
在已提权Python进程中不通过Shell启动提权子进程失败的问题求助
我现在碰到个棘手的问题:想通过Python的subprocess库启动一个需要管理员权限的可执行文件,但按照理想方式(shell=False,因为之后要杀掉这个进程,用shell的话不好单独终止目标程序)运行时,总是报错。
具体来说,当我用shell=True,或者先调用powershell.exe再启动目标程序时,一切都正常。但一旦设置shell=False,就会抛出OSError: [WinError 740] The requested operation requires elevation。更奇怪的是,我已经用ctypes确认当前Python进程是拥有管理员权限的——ctypes.windll.shell32.IsUserAnAdmin()返回的是True。
有没有朋友能帮我解答下这是怎么回事?或者有没有替代方案,既能不用shell=True启动程序,又能之后顺利杀掉它?
我的代码如下:
import ctypes import subprocess print(ctypes.windll.shell32.IsUserAnAdmin()) # 返回 true command = r'"C:\Users\First Last\HWiNFO64.exe"' proc = subprocess.Popen(command, shell=False)
报错信息:
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
备注:内容来源于stack exchange,提问作者Anonymus
相关产品推荐
相关产品推荐

