Skip to content

Instantly share code, notes, and snippets.

@sylvainpelissier
Forked from Preston-Landers/pyuac.py
Last active March 20, 2023 23:28
Show Gist options
  • Save sylvainpelissier/ff072a6759082590a4fe8f7e070a4952 to your computer and use it in GitHub Desktop.
Save sylvainpelissier/ff072a6759082590a4fe8f7e070a4952 to your computer and use it in GitHub Desktop.
pyuac - elevate a Python process with UAC on Windows compatible Python 3.
#!/usr/bin/env python
# -*- coding: utf-8; mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vim: fileencoding=utf-8 tabstop=4 expandtab shiftwidth=4
"""User Access Control for Microsoft Windows Vista and higher. This is
only for the Windows platform.
This will relaunch either the current script - with all the same command
line parameters - or else you can provide a different script/program to
run. If the current user doesn't normally have admin rights, he'll be
prompted for an admin password. Otherwise he just gets the UAC prompt.
Note that the prompt may simply shows a generic python.exe with "Publisher:
Unknown" if the python.exe is not signed.
This is meant to be used something like this::
if not pyuac.isUserAdmin():
return pyuac.runAsAdmin()
# otherwise carry on doing whatever...
See L{runAsAdmin} for the main interface.
"""
import sys, os, traceback, types
def isUserAdmin():
"""@return: True if the current user is an 'Admin' whatever that
means (root on Unix), otherwise False.
Warning: The inner function fails unless you have Windows XP SP2 or
higher. The failure causes a traceback to be printed and this
function to return False.
"""
if os.name == 'nt':
import ctypes
# WARNING: requires Windows XP SP2 or higher!
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
traceback.print_exc()
print("Admin check failed, assuming not an admin.")
return False
else:
# Check for root on Posix
return os.getuid() == 0
def runAsAdmin(cmdLine=None, wait=True):
"""Attempt to relaunch the current script as an admin using the same
command line parameters. Pass cmdLine in to override and set a new
command. It must be a list of [command, arg1, arg2...] format.
Set wait to False to avoid waiting for the sub-process to finish. You
will not be able to fetch the exit code of the process if wait is
False.
Returns the sub-process return code, unless wait is False in which
case it returns None.
@WARNING: this function only works on Windows.
"""
if os.name != 'nt':
raise RuntimeError("This function is only implemented on Windows.")
import win32api, win32con, win32event, win32process
from win32com.shell.shell import ShellExecuteEx
from win32com.shell import shellcon
python_exe = sys.executable
if cmdLine is None:
cmdLine = [python_exe] + sys.argv
elif type(cmdLine) not in (types.TupleType,types.ListType):
raise ValueError("cmdLine is not a sequence.")
cmd = '"%s"' % (cmdLine[0],)
# XXX TODO: isn't there a function or something we can call to massage command line params?
params = " ".join(['"%s"' % (x,) for x in cmdLine[1:]])
cmdDir = ''
showCmd = win32con.SW_SHOWNORMAL
lpVerb = 'runas' # causes UAC elevation prompt.
# print "Running", cmd, params
# ShellExecute() doesn't seem to allow us to fetch the PID or handle
# of the process, so we can't get anything useful from it. Therefore
# the more complex ShellExecuteEx() must be used.
# procHandle = win32api.ShellExecute(0, lpVerb, cmd, params, cmdDir, showCmd)
procInfo = ShellExecuteEx(nShow=showCmd,
fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
lpVerb=lpVerb,
lpFile=cmd,
lpParameters=params)
if wait:
procHandle = procInfo['hProcess']
obj = win32event.WaitForSingleObject(procHandle, win32event.INFINITE)
rc = win32process.GetExitCodeProcess(procHandle)
else:
rc = None
return rc
def test():
"""A simple test function; check if we're admin, and if not relaunch
the script as admin.""",
rc = 0
if not isUserAdmin():
print("You're not an admin.", os.getpid(), "params: ", sys.argv)
rc = runAsAdmin()
else:
print("You are an admin!", os.getpid(), "params: ", sys.argv)
rc = 0
input('Press Enter to exit.')
return rc
if __name__ == "__main__":
res = test()
sys.exit(res)
@rob4226
Copy link

rob4226 commented Nov 24, 2021

Cool works nice!! Using with Python 3.10. Just remember you need to install pip install pywin32 first. I made the changes that @remram44 mentioned also.

Also, if you want to hide the command window that pops up, try using https://github.com/wenshui2008/RunHiddenConsole like:

rc = runAsAdmin(['C:\\path\\to\\RunHiddenConsole.exe', 'C:\\path\\to\\your-command-as-admin.exe'])

@darkcicada23
Copy link

darkcicada23 commented Sep 27, 2022

@rob4226 and @remram44 how did it work for you guys? I'm using python 3.10 with pywin32 installed and i can't find shell from win32com:
win32com.shell.shell import ShellExecuteEx
from win32com.shell import shellcon
Error: Cannot find reference 'shell' in 'init.py'

I unistalled and installed several times pywin32 and still didn't work, i have the latest version 304

@remram44
Copy link

Check out https://github.com/Preston-Landers/pyuac for the updated version (which is also on PyPI: https://pypi.org/project/pyuac/)

@darkcicada23
Copy link

darkcicada23 commented Sep 27, 2022

thanks for the answer, but is still not working, i also can't find the files related to shell and shellcon in win32com in the installed files

@rob4226
Copy link

rob4226 commented Sep 27, 2022

@darkcicada23 Are you sure your Python environment is setup correctly? Try a virtual env to make sure:

python -m venv venv
./venv/Scripts/activate
pip install pywin32
python ./init.py

@darkcicada23
Copy link

darkcicada23 commented Sep 27, 2022

@rob4226 i also created another virtual env in pycharm and installed pywin32, so i don't think it's because of that.

When i try to import from win32com.shell import shellcon it says that there is no reference in init.py
Logically there must be something here in init file about shell and shellcon otherwise you can't call these objects.

For example if i create a new module named my_module and import it, if i want to use the function my_function
then i call my_module. and pycharm automatically shows my_function, and this case it's not available for win32com

If the statement from above works for you when you import it, do you have in init.py from win32com folder something about shell and shellcon or ShellExecuteEx?

@rob4226
Copy link

rob4226 commented Sep 28, 2022

@darkcicada23 You should not have to touch anything in the site-packages folder. If you installed pywin32 with pip into a virtual environment, anything installed with pip will automatically be included in the python import path. If importing is not working with simply the below, then your python environment is not setup correctly.

# your_file.py
# This is all you need to make this stuff available in your python script:
import win32api, win32con, win32event, win32process
from win32com.shell.shell import ShellExecuteEx
from win32com.shell import shellcon

If you want to try the examply file posted in this gist called pyuac.py (outside of pycharm), the below will work:

mkdir my-folder
cd my-folder
python -m venv venv
./venv/Scripts/activate
pip install pywin32
python ./pyuac.py

@remram44
Copy link

If you have issues with pywin32 try asking on their mailing-list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment