From 939b2c900773412f9bec383f437239e4b7996cab Mon Sep 17 00:00:00 2001 From: Benjamin Reed Date: Thu, 2 Mar 2023 17:22:49 -0500 Subject: [PATCH] scary batch file stuff --- RUNME.bat | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/RUNME.bat b/RUNME.bat index 4afa1e5..d81acca 100644 --- a/RUNME.bat +++ b/RUNME.bat @@ -1,4 +1,48 @@ -if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit ) +@ECHO OFF +setlocal EnableDelayedExpansion + +::net file to test privileges, 1>NUL redirects output, 2>NUL redirects errors +NET FILE 1>NUL 2>NUL +if '%errorlevel%' == '0' ( goto START ) else ( goto getPrivileges ) + +:getPrivileges +if '%1'=='ELEV' ( goto START ) + +set "batchPath=%~f0" +set "batchArgs=ELEV" + +::Add quotes to the batch path, if needed +set "script=%0" +set script=%script:"=% +IF '%0'=='!script!' ( GOTO PathQuotesDone ) + set "batchPath=""%batchPath%""" +:PathQuotesDone + +::Add quotes to the arguments, if needed. +:ArgLoop +IF '%1'=='' ( GOTO EndArgLoop ) else ( GOTO AddArg ) + :AddArg + set "arg=%1" + set arg=%arg:"=% + IF '%1'=='!arg!' ( GOTO NoQuotes ) + set "batchArgs=%batchArgs% "%1"" + GOTO QuotesDone + :NoQuotes + set "batchArgs=%batchArgs% %1" + :QuotesDone + shift + GOTO ArgLoop +:EndArgLoop + +::Create and run the vb script to elevate the batch file +ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" +ECHO UAC.ShellExecute "cmd", "/c ""!batchPath! !batchArgs!""", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" +"%temp%\OEgetPrivileges.vbs" +exit /B + +:START +::Remove the elevation tag and set the correct working directory +IF '%1'=='ELEV' ( shift /1 ) +cd /d %~dp0 -cd "%~dp0" %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned .\mw5-sync.ps1 \ No newline at end of file