open cmd with admin rights (Windows 10)

2019-05-15 06:55发布

问题:

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