How to get admin privilege in cmd for running sc.exe? - windows

I have created a batch file for starting a service using sc.exe. But it asks for admin privilege while running. Is there is any code that can be used in batch file to elevate cmd to admin level?

You can do this:
#echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if %errorlevel% NEQ 0 (GOTO askAdmin)
GOTO gotAdmin
:askAdmin
::batch is being ran as normal user
echo I'm not an admin yet
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
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%"
CD /D "%~dp0"
::batch is being ran as admin
echo Now I'm an admin!
pause

Press Windows Key + X and click Command Prompt (Admin). Click yes if the user control center ask for permission

Related

Elevation batch file script doesn't execute my code

The script that I am using is from here: https://stackoverflow.com/a/12264592/1016343
I have a batch file that is made for installing Office 365 using their click-to-run installer. It will first check if the directory that contains the XML configurations and setup.exe exists. Once that is confirmed, it will begin Elevate.cmd - Version 4. The only modifications I have made were removing the following lines:
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
REM Run shell as admin (example) - put here code as you like
ECHO %batchName% Arguments: P1=%1 P2=%2 P3=%3 P4=%4 P5=%5 P6=%6 P7=%7 P8=%8 P9=%9
cmd /k
My code is supposed to start after these lines:
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Whenever I run the batch file, I get a UAC prompt. After I accept the prompt, it closes. To confirm that my code is not executed, I added #echo HELLO! > hello.txt right before call setup.exe %config%, but I could not find the file in the directory. It was not in C:\Windows\System32 either.
Here is my batch file, Install.bat:
#echo off
set directory="<PATH EXCLUDED FOR PRIVACY. ALSO ON A NETWORK DRIVE.>"
if not exist %directory% goto :install_not_found
::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
:: see "https://stackoverflow.com/a/12264592/1016343" for description
::::::::::::::::::::::::::::::::::::::::::::
#echo off
:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set "batchPath=%~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 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 & cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
:: https://www.computerhope.com/forum/index.php?topic=75310.0
:choice
#echo Enter the number for which install you would like:
#echo [1] Office 32-Bit
#echo [c] Cancel
goto :listen
:listen
set /P c=^>
if /I "%c%" EQU "1" (
set config="/configure office_32.xml"
goto: install
)
if /I "%c%" EQU "C" goto :cancel
if /I "%c%" EQU "c" goto :cancel
#echo Invalid entry
goto :listen
:install_not_found
#echo Error: The installation folder could not be found.
goto :cancel
:cancel
#echo Cancelling Installation...
goto :eof
:install
pushd %directory%
elevate
#echo on
call setup.exe %config%
#echo off
popd
#pause

Building batch program to monitor, create, and delete battery logs

I am struggling with how to make the Energy-Reporter program save dates and rename the files. I am new to Batch programing. I started to learn this language when my OEMCreate DELL Latitude E5500 battery died after about a year. I am attempting to make a program that allows me to monitor and save battery statistic reports to my computer. I am hoping to add the ability to graph battery degradation over time. I attached the current code below. I need to figure out how to make a CMD based File manager.
#echo OFF
TITLE Energy-Reporter
set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (
set CUR_HH=0%time:~1,1%
set CUR_AP="AM"
)
if %CUR_HH% GEQ 12 (
set CUR_HH=CUR_HH-12
set CUR_AP="PM"
)
set CUR_NN=%time:~3,2%
set CUR_SS=%time:~6,2%
set CUR_MS=%time:~9,2%
set SUBFILENAME="%CUR_MM%-%CUR_DD%-%CUR_YYYY%_At_%CUR_HH%-%CUR_NN%-%CUR_SS% %CUR_AP%"
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>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"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
IF EXIST "C:\Program Files\Energy-Report\." (
ECHO DIRECTIORY FOUND
) else (
ECHO CREATING "C:\Program Files\Energy-Report\"
mkdir "C:\Program Files\Energy-Report\"
)
COLOR F4
:USER_SELECTER
CLS
ECHO ===============================================================
ECHO Battery logger
ECHO ===============================================================
ECHO Select an option:
Choice /C codq /N /M "[C]reate power log | [O]pen report | [D]elete log | [Q]uit"
If ErrorLevel 4 GoTo :EOF
If ErrorLevel 3 GoTo DELETE_LOG
If ErrorLevel 2 GoTo SAVED_LOGS
If ErrorLevel 1 GOTO REPORT_DATA
PAUSE
GOTO USER_SELECTER
:REPORT_DATA
cd "C:\Program Files\Energy-Report\"
powercfg -energy
REN "Energy-Report.html" "energy-report_%SUBFILENAME%.html"
PAUSE
GOTO USER_SELECTER
:SAVED_LOGS
cd "C:\Program Files\Energy-Report\"
DIR
PAUSE
GOTO USER_SELECTER
:DELETE_LOG
SET /P Entry_name=log to be erased
DELETE "C:\%Program Files%\Energy-Report\%Entry_name%"
PAUSE
GOTO USER_SELECTER
I have uploaded all of the previous versions of the code to my website for those who are interested in the code development.
If I do not get a chance to update the stack overflow version, The latest version is available by running the code snippet.
<embed style="width:100%;height:100%;"src="https://theelectronichandbook.tech/code/downloads/battery-logger-for-windows/latestcode.php?FRAME=iframe"></embed>

I need a simple Toggle script for Windows app called Defender Control

I am using this app called Defender Control. It works with command line switches "DefenderControl.exe /D" to disable Windows Defender and "DefenderControl.exe /E" to enable Windows Defender. I tried creating and modifying other toggle scripts but I cannot make one work. I have very little knowledge on scripting. I plan to add it to the right-click context menu to quickly disable Windows Defender without having to launch the app then disable it. Thanks in advance.
DefenderControl.exe /D
DefenderControl.exe /E
This writes the status to an alternate data stream (technically it's a file, but bound to the batch file), so the status survives reboots:
#echo off
REM get current status:
<"%~f0:status" set /p status=
echo Currently: %status%
if "%status%"=="D" (
defendercontrol.exe /E
echo E>"%~f0:status"
) else (
defendercontrol.exe /D
echo D>"%~f0:status"
)
For the very first run, the status is empty and handled the same, as it were E (goes to the else branch)
The solution for making the script work from the context menu was to elevate it.
#echo off
CLS
ECHO.
:init
setlocal DisableDelayedExpansion
set "batchPath=%~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 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%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
REM get current status:
<"%userprofile%\defendercontrol.status" set /p status=
echo Currently: %status%
if "%status%"=="D" (
defendercontrol.exe /E
echo E>"%userprofile%\defendercontrol.status"
) else (
defendercontrol.exe /D
echo D>"%userprofile%\defendercontrol.status"
)

How to automate installation using a bat file

I'm trying to install software by booting into a Windows installation to bring up the Command Prompt.
I can't run the msi there!
D:\setup>setup.msi
It says:
Access Is Denied
Is there a way to "automate" the installation with a .bat?
When I need UAC privilege escalation I use the following script:
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights V2
::::::::::::::::::::::::::::::::::::::::::::
#echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:init
setlocal DisableDelayedExpansion
set "batchPath=%~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%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
REM In this state it will Run shell as admin (example) - add your code here
ECHO %batchName% Arguments: %1 %2 %3 %4 %5 %6 %7 %8 %9
cmd /k
It will even give you the dialogue if the UAC settings requires it.
CREDIT : winhelponline.com
You cannot call a .msi file directly. You need to call msiexec /i setup.msi.
See: https://technet.microsoft.com/en-us/library/bb490936.aspx

How to run batch file using Runas command from a batch file

This is our batch file which we have this code but its not running the run.bat file.
Giving Error :
is not recognized as internal or external command
#echo off
SET Identity=%userdomain%\%username%
CALL :ICACLS "%Identity%"
SET mypath=%~dp0
echo %mypath:~0,-1%
runas /noprofile /user:%Identity% "cmd /K %mypath%\run.bat"
Echo Soft
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%"
CD /D "%~dp0"
::your code goes here which needs to be run with admin rights::

Resources