running a set up file in a batch as an admin? - batch-file

I created a batch to run specific commands, the code looks like this:
cd D:\projects\Project Stress Test\signed one\com0com\x64
setupc
pause
what i want is to run the setupc file as an admin?
i tried runas /user:<Name>\administrator commands but it didnt work.
is there any easy way to do that?

You could make the entire script run at admin level. Here is a batch function I use in my scripts.
#echo off
call :Admin xReturn true 1
echo.%xReturn%
goto End
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if needed.
:: NOTE: This will restart the script with Admin privs if Needed is set to true.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set xVBUAC=%Temp%\AdminUAC.vbs
set xSuccess=true
if not "%~3"=="" set xSuccess=%~3
:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set xAdmin=%xSuccess%
:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
if exist "%xVBUAC%" (
"%xVBUAC%"
rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
del "%xVBUAC%"
)
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof
:End

Related

Running a Batch with Admin Rights, but with pre saved User&Pass in the Batch to avoid windows asking for the Cridentials

I am already using the following Batch to open my Jar file with Admin Right:
#echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
-cp Jarfile.jar com.testApp
however after clicking on the Batch file, the windows will ask me to insert the Username and password.
The Question is , is there any way to give the user and pass inside the Batch file to open the Jar file but without windows asking for user and pass?

Store the current directory in a variable to be used later

I have a program that asks for admin privileges, stops some services, deletes a few files that couldn't be deleted if the services were running, restarts the services, and goes to a hard coded address where I have a vbs script I want to run next.
My problem is I'm about to put it on more computers, and I don't want to edit the hard coded address each time I set it up. I know that when you open a batch file, it starts at the directory where its located. I would like to save that in a variable so i can come back to it later to run the vbs script. Is there any way to do this?
Here is my code:
problem is at the bottom of the script
#echo off
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 )
#echo off
: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"
#echo off
net stop wuauserv
net stop bits
---save directory here--
cd /
cd Windows
echo To clean Windows Update, we need to wipe a folder. Wiping
rd /s SoftwareDistribution
mkdir SoftwareDistribution
echo The folder has been wiped.
pause
echo.
cd /
net start wuauserv
net start bits
---go to saved directory---
popup <-- VBS Script
pause
exit
A big thank you to #Compo for suggesting to me the use of ‘’’%~dp0’’’
It fixed pretty much everything.

Can Window's ShellExecute function accept WSF script arguments?

The batch hybrid script below aims to auto invoke Administrator privileges before running some tasks requiring elevated rights. It does popup the UAC prompt, but regardless of the user choice Admin privileges aren't granted.
I wonder if Window's ShellExecute function used in it can accept WSF arguments or other expandable parameters? In this case its the batch file name. If it can, how the script should be changed for that without restructuring aimed at using a different method?
<!-- : Begin batch script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
CD /D "%~dp0" & echo "%*"
set "dir=%temp%\Unzip"
set "file=%USERPROFILE%\Downloads\archive.zip"
if not "%1"=="ADR" (call :GetAdminRights
if defined adm cscript //nologo "%~f0?.wsf" //job:ADM "%~nx0")
>nul 2>&1 net file && (echo/ & echo "!errorlevel!") || ^
(echo/ & echo "!errorlevel!" & goto :end)
:: add your code here
echo Performing admin tasks
echo Hello >C:\test.txt
:end
timeout 5
exit /b
:GetAdminRights
REM Check for permissions
>nul 2>&1 net file
REM If error flag set, user don't have admin permissions
if '!errorlevel!' NEQ '0' (echo Requesting administrative privileges...
set "adm=0"
echo/ & echo "!errorlevel!" "%~nx0" "%~dp0" & echo/)
exit /b
----- Begin wsf script --->
<package>
<job id="ADM"><script language="VBScript">
Set UAC = CreateObject("Shell.Application")
WScript.Echo wscript.Arguments(0)
UAC.ShellExecute "cmd.exe", "/c ""wscript.Arguments(0)"" ADR", "", "runas", 1
</script></job>
</package>
:: Error in UAC Prompt (shown in details. Can't expand batch name correctly.)
Program location: "C:\Windows\System32\cmd.exe /c "wscript.Arguments(0)" ADR
Yes, it can. The trick is to submit in the cscript call all Cmd.exe arguments required to restart Cmd in Administrator mode, and read them properly in the WSF section of the batch.
<!-- : Begin batch script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
CD /D "%~dp0" & echo "%*"
set "dir=%temp%\Unzip" & set "file=%USERPROFILE%\Downloads\archive.zip"
if not "%1"=="ADR" (call :GetAdminRights
if defined adm cscript //nologo "%~f0?.wsf" //job:ADM "/c" "%~sf0" "ADR" )
echo/ & >nul 2>&1 net file && (echo "!errorlevel!" Got admin rights & echo/) ^
|| (echo "!errorlevel!" No admin rights & goto :end)
:: add your code here
echo Performing admin tasks
echo Hello >C:\tst.txt
:end
timeout /t 5 >nul
exit /b
:GetAdminRights
REM Check for permissions
echo/ & >nul 2>&1 net session && (echo Got admin rights) ^
|| (echo No admin rights) & echo/
REM If error flag set, user don't have admin permissions
if '!errorlevel!' NEQ '0' (echo Requesting admin rights...
set "adm=0" & echo/ & echo "!errorlevel!" "%~nx0" "%~dp0" & echo/)
exit /b
----- Begin wsf script --->
<package>
<job id="ADM"><script language="VBScript">
Set UAC = CreateObject("Shell.Application")
args = ""
For Each strArg in WScript.Arguments
args = args & strArg & " "
Next
WScript.Echo args
UAC.ShellExecute "cmd.exe", args, "", "runas", 1
</script></job>
</package>
Here is a batch script 'Elevate.bat' to execute as Administrator any command passed in arguments string.
If the command contains special characters, escape them by '^'.
For instance:
Elevate whoami /groups ^| find "S-1-16-12288"`
Elevate.bat:
<!-- : --- Self-Elevating Batch Script ---------------------------
#whoami /groups | find "S-1-16-12288" > nul && goto :admin
#set "ELEVATE_CMDLINE=cd /d "%cd%" & call "%~f0" %*"
#cscript //nologo "%~f0?.wsf" //job:Elevate & exit /b
-->
<job id="Elevate"><script language="VBScript">
Set objShell = CreateObject("Shell.Application")
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objWshProcessEnv = objWshShell.Environment("PROCESS")
strCommandLine = Trim(objWshProcessEnv("ELEVATE_CMDLINE"))
objShell.ShellExecute "cmd", "/c " & strCommandLine, "", "runas"
</script></job>
:admin -----------------------------------------------------------
#echo off
echo Running as elevated user.
echo Script file : %~f0
echo Arguments : %*
echo Working dir : %cd%
echo.
:: administrator commands here
:: e.g., run shell as admin
%*
pause
I use it to create symbolic links like this:
Elevate.bat mklink /d common g:\PerforceData\devel\common
Elevate.bat mklink build.xml g:\PerforceData\devel\build.xml

How to run a Command Prompt run as a Administrator using batch file

I want to run a jar file, jar file contents some external command so required it as run administrator, so I want it to run without using batch file with run as administrator
You can run using either right click on it and select run as administrator or following code content batch file code for run as administrator.
#echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
C:
test.jar

Start another .bat in .bat with wait option

I have little problem, because I must in my first .bat start another .bat with admin pass.
There I have problem, because when i use start /wait to start second .bat when all operations finish second cmd window doesn't closing and view directory. Or when I use this method in second bat wants admin pass whereupon open thierd cmd window and in second vie directory.
First bat:
#ECHO OFF
start /wait test2.bat
pause
Second bat
#echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
call notepad.exe
pause

Resources