I'm attempting to create a utility tool via Python 3.x for the Windows 10 command-line. Since it will better format general command-line commands into more user-friendly menus, I want it to require elevated permissions through UAC when it runs.
I'm using the ctypes
method described here, and it does indeed have Python's executable request UAC elevation.
However, since a lot of the things I'll be writing menus and the like for will require (or be heavily limited without) these elevated permissions, I want the script to exit (preferably through sys.exit
) if it doesn't find any.
In the ctypes
method I mentioned, it should run as follows;
It defines a function
is_admin()
, which gets the value ofctypes.windll.shell32.IsUserAnAdmin()
, and if it's 0, returns false instead.is_admin()
is called in a conditional, and if it gets false, it attempts to execute the command-line command to re-run the script as an executable usingShellExecuteW
and some variables fromsys
;ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv[0]), None, 1)
In my code, I have the above conditional with the addition of a variable elevReq
that I set to true;
if is_admin():
success("Already running as administrator!") # "success" and "warn" are defined earlier
elevReq = True
else:
warn("Requesting administrative permissions...", False)
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv[0]), None, 1)
elevReq = True
I follow it up with another conditional that checks to see if elevReq
is true and is_admin()
is false, to see if the user selected "no" on UAC's pop-up -- if it is, it should throw an error about the lack of elevated privileges, and then quit;
if elevReq and is_admin() == False:
error("[FATAL] Elevation was not given! Stopping...", True)
sys.exit(1)
The problem I'm having is that the given method doesn't seem to actually be elevating permissions of Python. UAC does pop up, but when any option is selected, it doesn't seem to matter, as the above condition fires anyway. Manually running the script in an elevated command prompt from the start doesn't have this issue.
Is this an issue with the script not re-loading when it should? If not, why is it exiting anyway?