Run process as admin with subprocess.run in python
Asked Answered
Y

4

17

Is there a way of passing some runas=True arg to a subprocess.run function in python? I want to run a process as admin (elevate it). Thanks for answers :)\

EDIT: Using Windows OS.

Yasmineyasu answered 19/11, 2017 at 18:23 Comment(0)
T
24

Windows has a command line utility "Run as", which can be used as

  runas [{/profile | /noprofile}] [/env] [{/netonly | /savecred}] [/smartcard] [/showtrustlevels] [/trustlevel] /user:<UserAccountName> "<ProgramName> <PathToProgramFile>"

for further reference https://technet.microsoft.com/en-us/library/cc771525.aspx

You can use this in code like below

import subprocess as sp

prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
prog.stdin.write('password')
prog.communicate()
Triclinic answered 20/11, 2017 at 5:18 Comment(1)
Note that this only works with the built-in Administrator user account, not generally with users that are part of the Administrators group. In practice, the built-in Administrator account is often disabled for security reasons.Iredale
J
4

If you want to run a command as the same user but with admin privilege

Please refer to this solution:

os.system(r'''
Powershell -Command "& { Start-Process \"notepad.exe\"
 -ArgumentList @(\"C:\\Windows\\System32\\drivers\\etc\\hosts\")
 -Verb RunAs } " '''

The original answer can be found here https://superuser.com/a/753600/1088510

Jinja answered 10/12, 2021 at 7:11 Comment(0)
C
4

There are three approaches:

  1. Using runas as shown in this answer. The downside of this approach is that it uses the Administrator account instead of the current user's Administrator privileges. This does not work well if you plan to deploy your software to users.
  2. Use ShellExecute as discussed in this question to start your subprocess. The downside is that you won't be able to work with stdin/stdout/stderr.
  3. Use JetBrains' WinElevator (signed launcher.exe and elevator.exe are available here). The downside of this approach is that you need to ship two additional ~150kb binaries, the upside is that you can interact with stdin/stdout/stderr.
Crofoot answered 21/8, 2022 at 10:51 Comment(0)
S
-3

As others have suggested you can achieve this using powershell. These are my PS functions I use to elevate to Admin:

# ========================================= Admin Rights =======================================================
# Usage: asAdmin $PSCommandPath
function asAdmin
{
    [string]$cmdPath = $args[0]
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$cmdPath`"" -Verb RunAs; exit }
}

#-noexit
function asAdminWithSTA
{
    [string]$cmdPath = $args[0]
    if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-sta  -NoProfile -ExecutionPolicy Bypass -File `"$cmdPath`"" -Verb RunAs; exit }
}
Sunwise answered 29/12, 2022 at 6:14 Comment(2)
Sorry but the OP was looking for a solution in PythonRecidivism
@Recidivism My point was you can call powershell from Python which in turn could run another exe with elevated privilegesSunwise

© 2022 - 2024 — McMap. All rights reserved.