Run process as admin with in python

Posted on

Question :

Run process as admin with in python

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

EDIT: Using Windows OS.

Answer #1:

since OS is not specified, I will take the example of MS Windows OS

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

You can use this in code like below

import subprocess as sp

prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
Answered By: Jakub Bláha

Leave a Reply

Your email address will not be published. Required fields are marked *