i tried posting a question on stackoverflow here, but all i got was four different “solutions” that informed me about things that were of no bearing to the issue and a couple of questions about what i was going to use it for. working as expected, and when the wife said those folks were rude and not me, i told her if i had posted it here i’d have three answers by now. then it clicked, so here we are.
basically, i am making a menu driven batch file that does some things that require elevation to admin rights and is supposed to popup the box asking for the password and then elevate the command prompt. i got some code from one site that worked, then i left it alone for six months and came back to it and it doesn’t work now. so i got some other code that did the same thing that was a bit longer and it doesn’t even pop up the uac box.
this is the batch file:
echo off
cls
:: check if we are admin
:-------------------------------------
REM → Check for permissions
nul 2>&1 “%SYSTEMROOT%\system32\cacls.exe” “%SYSTEMROOT%\system32\config\system”
REM → If error flag set, we do not have admin.
if ‘%errorlevel%’ NEQ ‘0’ (
echo Requesting administrative privileges…
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^(“Shell.Application”^) > “%temp%\getadmin.vbs”
echo UAC.ShellExecute “%~s0”, “”, “”, “runas”, 1 >> “%temp%\getadmin.vbs”
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist “%temp%\getadmin.vbs” ( del “%temp%\getadmin.vbs” )
pushd “%CD%”
:--------------------------------------
:menu
cls
ECHO.
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO What would you like to do? Type 4 to exit.
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO.
ECHO 1 - Clear the print spooler
ECHO 2 - Restore network connectivity
ECHO 3 - Get computer information
ECHO 4 - Exit
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO print
IF %M%==2 GOTO network
IF %M%==3 GOTO compinfo
IF %M%==4 GOTO EOF
:print
@echo off
net stop spooler
pause
cd c:\windows\system32\spool\printers
pause
whoami
pause
::del /Q .
pause
net start spooler
goto menu
:network
@echo off
ipconfig /release
ipconfig /renew
ipconfig /flushdns
ipconfig /registerdns
netsh dump
nbtstat -R
netsh int ip reset reset.log
netsh winsock reset
echo You need to restart now to finish configuration changes…
::set /p answer=Restart now?
::if %answer%=“y” shutdown /r /t 0 else
::if %answer%=“yes” shutdown /r /t 0 else
pause
goto menu
:compinfo
@echo off
REM set variables
set computer=
set system=
set manufacturer=
set model=
set serialnumber=
set osname=
set sp=
set cstring=
set ustring=
set pstring=
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% OS Get csname /value’) do SET computer=%%A
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% OS Get csname /value’) do SET system=%%A
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% ComputerSystem Get Manufacturer /value’) do SET manufacturer=%%A
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% ComputerSystem Get Model /value’) do SET model=%%A
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% Bios Get SerialNumber /value’) do SET serialnumber=%%A
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% os get Name /value’) do SET osname=%%A
FOR /F “tokens=1 delims=‘|’” %%A in (“%osname%”) do SET osname=%%A
FOR /F “tokens=2 delims=‘=’” %%A in (‘wmic %cstring% %ustring% %pstring% os get ServicePackMajorVersion /value’) do SET sp=%%A
echo done!
echo ----------------
echo System Name: %system%
echo Manufacturer: %manufacturer%
echo Model: %model%
echo Serial Number: %serialnumber%
echo Operating System: %osname%
echo Service Pack: %sp%
echo ----------------
REM Generate file
SET file=“%~dp0%computer%.txt”
echo ---------------- > %file%
echo Details For %computer%: >> %file%
echo System Name: %system% >> %file%
echo Manufacturer: %manufacturer% >> %file%
echo Model: %model% >> %file%
echo Serial Number: %serialnumber% >> %file%
echo Operating System: %osname% >> %file%
echo Service Pack: %sp% >> %file%
echo ---------------- >> %file%
echo File created at %file%
REM request user to push any key to continue
pause
goto menu
and this is the uac code i used after that didn’t work:
:: check if we are admin
:-------------------------------------
::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
:: see “windows - How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required? - Stack Overflow” for description
::::::::::::::::::::::::::::::::::::::::::::
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set “batchPath=%~dpnx0”
rem this works also from cmd shell, other than %~0
for %%k in (%0) do set batchName=%%~nk
set “vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs”
setlocal EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if ‘%errorlevel%’ == ‘0’ ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if ‘%1’==‘ELEV’ (echo ELEV & shift /1 & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
ECHO Set UAC = CreateObject^(“Shell.Application”^) > “%vbsGetPrivileges%”
ECHO args = "ELEV " >> “%vbsGetPrivileges%”
ECHO For Each strArg in WScript.Arguments >> “%vbsGetPrivileges%”
ECHO args = args ^& strArg ^& " " >> “%vbsGetPrivileges%”
ECHO Next >> “%vbsGetPrivileges%”
if ‘%cmdInvoke%’==‘1’ goto InvokeCmd
ECHO UAC.ShellExecute “!batchPath!”, args, “”, “runas”, 1 >> “%vbsGetPrivileges%”
goto ExecElevation
:InvokeCmd
ECHO args = “/c “”” + “!batchPath!” + “”" " + args >> “%vbsGetPrivileges%”
ECHO UAC.ShellExecute “%SystemRoot%%winSysFolder%\cmd.exe”, args, “”, “runas”, 1 >> “%vbsGetPrivileges%”
:ExecElevation
“%SystemRoot%%winSysFolder%\WScript.exe” “%vbsGetPrivileges%” %*
exit /B
:gotPrivileges
setlocal %~dp0
if ‘%1’==‘ELEV’ (del “%vbsGetPrivileges%” 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
REM Run shell as admin (example) - put here code as you like
sorry about the huge post but we don’t have code formatting here like they do at stackoverflow, go figure. this is the error message i get from the first menu:
this is from the second uac elevation code:
this error comes at the end of the script and the uac box never pops up. it seems like there is something wrong with the script but i can’t find it to check. it has to be talking about the vb script it’s calling because this batch file doesn’t have that many characters in line 1. any help is appreciated. maybe @FieldEffect or @Nitt?