Python调用管理员权限执行脚本

1, 代码内调用管理员权限执行一个exe文件

if is_admin():
    os.startfile("system-jg_new.exe")
else:
    if sys.version_info[0] == 3:
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
        os.startfile("system-jg_new.exe")
    else:  # in python2.x
        ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(__file__), None, 1)

2, 代码内调用管理员权限执行cmd命令
if is_admin():
      os.system(command) 
else:
   if sys.version_info[0] == 3:
     ctypes.windll.shell32.ShellExecuteW(None, "runas", "cmd.exe","/C %s"%command, None, 1)
   else:  # in python2.x
         ctypes.windll.shell32.ShellExecuteW(None, u"runas","cmd.exe","/C %s"%command, None, 1)



参考博文: https://www.cnblogs.com/leigepython/p/10532321.html
posted @ 2021-09-26 11:30  2020不在低调  阅读(2855)  评论(0)    收藏  举报