Skip to content

Instantly share code, notes, and snippets.

@golflima
Forked from Preston-Landers/pyuac.py
Last active July 19, 2025 13:52
Show Gist options
  • Save golflima/bc4ac4bbe044d2465c57d898031a50d6 to your computer and use it in GitHub Desktop.
Save golflima/bc4ac4bbe044d2465c57d898031a50d6 to your computer and use it in GitHub Desktop.

Revisions

  1. golflima revised this gist Mar 12, 2018. 1 changed file with 10 additions and 9 deletions.
    19 changes: 10 additions & 9 deletions pyuac.py
    Original file line number Diff line number Diff line change
    @@ -36,13 +36,14 @@ def isUserAdmin():
    """

    if os.name == 'nt':
    import ctypes
    import win32security
    # WARNING: requires Windows XP SP2 or higher!
    try:
    return ctypes.windll.shell32.IsUserAnAdmin()
    adminSid = win32security.CreateWellKnownSid(win32security.WinBuiltinAdministratorsSid, None)
    return win32security.CheckTokenMembership(None, adminSid)
    except:
    traceback.print_exc()
    print "Admin check failed, assuming not an admin."
    print("Admin check failed, assuming not an admin.")
    return False
    else:
    # Check for root on Posix
    @@ -64,7 +65,7 @@ def runAsAdmin(cmdLine=None, wait=True):
    """

    if os.name != 'nt':
    raise RuntimeError, "This function is only implemented on Windows."
    raise RuntimeError("This function is only implemented on Windows.")

    import win32api, win32con, win32event, win32process
    from win32com.shell.shell import ShellExecuteEx
    @@ -75,7 +76,7 @@ def runAsAdmin(cmdLine=None, wait=True):
    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."
    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:]])
    @@ -112,16 +113,16 @@ def test():
    the script as admin.""",
    rc = 0
    if not isUserAdmin():
    print "You're not an admin.", os.getpid(), "params: ", sys.argv
    print("You're not an admin.", os.getpid(), "params: ", sys.argv)
    #rc = runAsAdmin(["c:\\Windows\\notepad.exe"])
    rc = runAsAdmin()
    else:
    print "You are an admin!", os.getpid(), "params: ", sys.argv
    print("You are an admin!", os.getpid(), "params: ", sys.argv)
    rc = 0
    x = raw_input('Press Enter to exit.')
    x = input('Press Enter to exit.')
    return rc


    if __name__ == "__main__":
    res = test()
    sys.exit(res)
    sys.exit(res)
  2. @Preston-Landers Preston-Landers created this gist Feb 20, 2015.
    127 changes: 127 additions & 0 deletions pyuac.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,127 @@
    #!/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)
    #print "Process handle %s returned code %s" % (procHandle, rc)
    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(["c:\\Windows\\notepad.exe"])
    rc = runAsAdmin()
    else:
    print "You are an admin!", os.getpid(), "params: ", sys.argv
    rc = 0
    x = raw_input('Press Enter to exit.')
    return rc


    if __name__ == "__main__":
    res = test()
    sys.exit(res)