Adrian Ivasku
Adrian Ivasku

Reputation: 1118

open cmd with admin rights (Windows 10)

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

Upvotes: 4

Views: 8728

Answers (3)

Johann Chang
Johann Chang

Reputation: 1391

You can launch the subprocess via NirCmd.

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

Upvotes: 0

xs6
xs6

Reputation: 890

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

Upvotes: 1

user1440897
user1440897

Reputation:

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)

Upvotes: 0

Related Questions