Skip to content

Instantly share code, notes, and snippets.

@masamitsu-murase
Created March 7, 2018 14:40
Show Gist options
  • Save masamitsu-murase/1f93636ca11901ab0cd0591509c6d031 to your computer and use it in GitHub Desktop.
Save masamitsu-murase/1f93636ca11901ab0cd0591509c6d031 to your computer and use it in GitHub Desktop.
Run Python script as Administrator.
@goto BEGINNING_OF_CMD
# -*- coding: UTF-8 -*-
"""
:BEGINNING_OF_CMD
@echo off
cd /d "%~dp0"
set PYTHON=python.exe
%PYTHON% -c "import ctypes, sys, ctypes.wintypes; ctypes.windll.shell32.IsUserAnAdmin.restype = ctypes.wintypes.BOOL; sys.exit(ctypes.windll.shell32.IsUserAnAdmin())"
if ERRORLEVEL 2 goto :ERROR
if ERRORLEVEL 1 goto :ADMIN
goto :NON_ADMIN
:ADMIN
%PYTHON% -x "%~nx0"
exit /b %ERRORLEVEL%
:NON_ADMIN
powershell Start-Process "%~0" -Verb runas
exit /b 0
:ERROR
echo IsUserAnAdmin failed.
exit /b 1
"""
print("hoge")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment