open cmd with admin rights (Windows 10)

2019-05-15 06:21发布

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

3条回答
Animai°情兽
2楼-- · 2019-05-15 07:00

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

查看更多
3楼-- · 2019-05-15 07:12

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)
查看更多
Fickle 薄情
4楼-- · 2019-05-15 07:12

You can launch the subprocess via NirCmd.

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

查看更多
登录 后发表回答