The value of the variable is changed - cmd

I'm writing my first script with assigning the current script path to the crtFile variable
#echo off
SET "crtFile=%cd%"
powershell -inputformat none -outputformat none -NonInteractive -Command Add-MpPreference -ExclusionPath "%crtFile%"
But then the variable was changed after doing a few commands to get Admin rights below
:: Elevating UAC Administrator Privileges
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if "%errorlevel%" NEQ "0" (
echo: Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo: UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs" & exit
)
Is there a way to assign an immutable value to this variable? Thanks

Related

Windows batchfile asking for admin rights and download a file in curl fails

I have a batch file like this
#echo off
::::::::::::::::::::::::::::::::::::::::::::
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' (goto gotPrivileges) else (goto getPrivileges)
:getPrivileges
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
echo If strArg = WScript.Arguments.Item^(0^) Then d = Left^(strArg, InStrRev^(strArg,"\"^) - 1^) >> "%temp%\getadmin.vbs"
echo args = args ^& " " ^& strArg >> "%temp%\getadmin.vbs"
echo Next >> "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", ^("/c start /D """ ^& d ^& """ /B" ^& args ^& " ^& exit"^), , "runas", 4 >> "%temp%\getadmin.vbs"
cscript "%temp%\getadmin.vbs" ""%~s0"" %*
del /q "%temp%\getadmin.vbs"
exit /b
:gotPrivileges
:: Your code here
echo "Downloading old version of OrthoSelect..."
curl -o ./6.7.5.zip "https://github.com/ruellm/OrthoSelect_6.7.5/archive/refs/tags/6.7.5.zip"
pause
It ask for an admin permission and executes curl to download the file, but unfortunately, even if the admin rights is given, the zip file downloaded is only 1Kb, the entire zip file is not downloaded properly,
The console window do not show error as well.
Anyone can help whats wrong?
Links to files on Github are not direct links, so if you try to simply curl the file, you'll end up only downloading the redirect page instead. If you use a text editor to open the file that you've downloaded, you'll see
<html><body>You are being redirected.</body></html>
This should tell you that you need to use the -L flag to follow redirects:
curl -L "https://github.com/ruellm/OrthoSelect_6.7.5/archive/refs/tags/6.7.5.zip" -o 6.7.5.zip

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

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::

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

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

Resources