4

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

Adrian Ivasku
  • 1,118
  • 3
  • 16
  • 31
  • Any command you run under a `cmd.exe` run as administrator mode, will also run as a administrator. Correct me if I'm wrong but that should be the case. – Torxed Dec 28 '15 at 20:46
  • https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script – Cory Kramer Dec 28 '15 at 20:46
  • @Torxed No, atleast in my case it is not. Example if I opent cmd.exe from the start menu and type netsh command I get the error message about the lack of administrator rights. – Adrian Ivasku Dec 28 '15 at 20:52
  • @CoryKramer I have seen this ( and tryed Jorenko`s solution but it doesn`t work) I need separate of python if there is a command to request administrator right. Something like sudo in Linux. – Adrian Ivasku Dec 28 '15 at 20:53
  • Adrian, if you right-click cmd.exe and select "run as administrator" then you will get an administrative command window. (There are shortcuts, but it depends on which version of Windows you are running.) – Harry Johnston Dec 29 '15 at 02:23

3 Answers3

1

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

xs6
  • 860
  • 8
  • 18
  • This seems to work! It doesn`t work with regedit, but it works with netsh command! Thank you! – Adrian Ivasku Dec 28 '15 at 21:05
  • so you want know to set a password for the hidden admin account ? – xs6 Dec 28 '15 at 21:10
  • I have managed to solve it with my user name and password. Thank you. – Adrian Ivasku Dec 28 '15 at 21:39
  • OK! As Tipp: If you want to set Administrators password: Press Windows-Flag-Button+R --> compmgmt.msc --> Local Users and Groups...there you can set. – xs6 Dec 28 '15 at 22:05
  • @AdrianIvasku, I'm sorry to say this answer is wrong for a stock Windows installation. The "Administrator" account (not just an account in the Administrators group) is initially disabled because it's not subject to UAC restrictions. For any other account in the Administrators group, the stock UAC configuration causes the account to be logged on with a split token. You have to elevate to get the full administrator token, which runas.exe *does not do*. – Eryk Sun Dec 28 '15 at 22:56
  • The account is a speciall account, which is not visible by default. But it is dont a disabled account! Dont anwer if u dont know what you are talking! You can give the Administrator a password as i discriped, also you can delete all users and log in as administrator ... – xs6 Dec 28 '15 at 23:28
  • The Administrator account *is* disabled by default on all current versions of Windows. But you can re-enable it, and the GUI password changer might do that automatically. IIRC, Windows also re-enables the Administrator automatically if it is the only administrator account remaining. – Harry Johnston Dec 29 '15 at 02:26
  • Here's an old article on Vista security changes: [New ACLs Improve Security in Windows Vista](https://technet.microsoft.com/en-us/magazine/2007.06.acl.aspx) (2007). Among other topics, this article explains the rationale for disabling the Administrator account (i.e. RID 500). When you try to log on using a disabled account, the error code is `ERROR_ACCOUNT_RESTRICTION` (1327), which is what runas.exe sets as its exit code in this case. You can see for yourself by first running `net user Administrator /active:no`. – Eryk Sun Dec 29 '15 at 03:44
0

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)
Community
  • 1
  • 1
  • I have tried something like this and it doesn`t work. No exceptions but the administrator levels are not gained. It exits in the IF statement block. – Adrian Ivasku Dec 28 '15 at 21:12
  • @AdrianIvasku, this is the way to elevate interactively with a UAC prompt. This is not how to elevate silently. For that you can create a task scheduled to run with highest privileges. Then invoke the task on demand (e.g. via [schtasks.exe](https://technet.microsoft.com/en-us/library/cc725744)). – Eryk Sun Dec 28 '15 at 23:14
0

You can launch the subprocess via NirCmd.

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

Johann Chang
  • 1,281
  • 2
  • 14
  • 25