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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM