open cmd with admin rights (Windows 10)

假如想象 提交于 2020-01-13 18:14:32

问题


I have my own python script that manages the IP address on my computer. Mainly it executes the netsh command in the command line (windows 10) which for you must have administrator rights.

It is my own computer, I am the administrator and when running the script I am already logged in with my user (Adrian) which is of type administrator.

I can`t use the right click and "run as administrator" solution because I am executing my netsh command from my python script.

Anybody knows how to get "run as administrator" with a command from CMD ?

Thanks


回答1:


Try something like this: runas /user:administrator regedit.




回答2:


You can request UAC elevation from within your Python script.

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)
    sys.exit(0)
else:
    print "I'm elevated!"
    sys.exit(0)



回答3:


You can launch the subprocess via NirCmd.

http://www.nirsoft.net/utils/nircmd.html



来源:https://stackoverflow.com/questions/34500369/open-cmd-with-admin-rights-windows-10

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!