====== Eseguire elevazione permessi su windows ======
tratto da [[https://stackoverflow.com/questions/19672352/how-to-run-script-with-elevated-privilege-on-windows#19694262]]
Ho cercato di adattare il codice a python3.x
La funzione nuda e cruda che fa il lavoro sporco รจ questa:
#!python
# coding: utf-8
import sys, os
import ctypes
def run_as_admin(argv=None, debug=False):
shell32 = ctypes.windll.shell32
if shell32.IsUserAnAdmin():
return True
executable='mioprog.exe'
argument_line='-run'
ret = shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
Lancia mioprog.exe con argomento "-run"
Di seguito il codice integrale:
#!python
# coding: utf-8
import sys
import ctypes
def run_as_admin(argv=None, debug=False):
shell32 = ctypes.windll.shell32
if argv is None and shell32.IsUserAnAdmin():
return True
if argv is None:
argv = sys.argv
if hasattr(sys, '_MEIPASS'):
# Support pyinstaller wrapped program.
#arguments = map(unicode, argv[1:])
arguments = argv[1:]
else:
#arguments = map(unicode, argv)
arguments = argv
argument_line = u' '.join(arguments)
executable = sys.executable
if debug:
print ('Command line: ', executable, argument_line)
ret = shell32.ShellExecuteW(None, u"runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
if __name__ == '__main__':
ret = run_as_admin()
if ret is True:
print ('I have admin privilege.')
input('Press ENTER to exit.')
elif ret is None:
print ('I am elevating to admin privilege.')
input('Press ENTER to exit.')
else:
print ('Error(ret=%d): cannot elevate privilege.' % (ret, ))