简体   繁体   English

在 python 脚本中请求 UAC 提升

[英]Requesting UAC elevation in a python script

I need my python script to have elevated UAC privileges.我需要我的 python 脚本来提升 UAC 权限。 I have tried again and again and can NOT figure it out.我一次又一次地尝试,无法弄清楚。

This is my code right now:这是我现在的代码:

from win32com.shell.shell import ShellExecuteEx
from win32com.shell import shellcon
import win32process, win32event
import win32con
from ntsecuritycon import *

ASADMIN = 'asadmin'

if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    ShellExecuteEx(fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
                    lpVerb="runas",
                    lpFile=sys.executable,
                    lpParameters=params,
                    nShow=win32con.SW_SHOW)
    sys.exit(0)

Now, the information I have so far is that this will run without errors, and it does reopen itself.现在,到目前为止,我所掌握的信息是,这将运行而不会出错,并且它会自行重新打开。 The reopened script returns重新打开的脚本返回

["c:\\users\\justin\\UAC.py", "asadmin"]

for为了

sys.argv

, but it still won't let me perform admin tasks. ,但它仍然不会让我执行管理任务。

Although help with fixing this code would be optimal, I'm also ready to try anything else that works, thanks!虽然修复此代码的帮助是最佳的,但我也准备尝试其他任何有效的方法,谢谢!

Use the following:使用以下内容:

import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'

if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    print("I am root now.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM