Define privilege in batch File - batch-file

I need to make a batch(.bat) file in which i want to give an administrator privilege..
mean every time when it open, it is in Run as an administrator Mode...
hope you understand...

Create the batch file, then right click it in Windows Explorer. Go to the Compatibility tab and click "Run this program as an administrator"

This could be potentially dangerous and I don't really know what you need, but use this code...
#ECHO OFF
:: Automatically elevate...
#ECHO OFF
SETLOCAL
:: Check if script was run with administrator privilages
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
:: If the error flag is set, we do not have administrative privileges.
IF "%ERRORLEVEL%"=="0" 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" )
CD /D "%~dp0"
ENDLOCAL
GOTO SCRIPTSTART
:: THE ACTUAL SCRIPT STARTS HERE
:SCRIPTSTART
Make sure you have VBScript before you use it

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.

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

How to create a batch file to run cmd as administrator

I need to run a batch file which needs to register a DLL. The DLL registration is failing because the Batch file is not starting the command prompt as "administrator".
Is there any way to start the "Command Prompt" as administrator through the batch file.
Environment: Win7/Vista
This script does the trick! Just paste it into the top of your bat file. If you want to review the output of your script, add a "pause" command at the bottom of your batch file.
This script is now slightly edited to support command line args.
#echo off
:: BatchGotAdmin
::-------------------------------------
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"
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"
::--------------------------------------
::ENTER YOUR CODE BELOW:
You might have to use another batch file first to launch the second with admin rights.
In the first use
runas /noprofile /user:mymachine\administrator yourbatchfile.bat
Upon further reading, you must be able to type in the password at the prompt. You cannot pipe the password as this feature was locked down for security reasons.
You may have more luck with psexec.
Press Ctrl+Shift and double-click a shortcut to run as an elevated process.
Works from the start menu as well.
(This is based on #DarkXphenomenon's answer, which unfortunately had some problems.)
You need to enclose your code within this wrapper:
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
echo %~nx0: running payload with parameters:
echo %*
echo ---------------------------------------------------
cd /d %2
shift
shift
rem put your code here
rem e.g.: perl myscript.pl %1 %2 %3 %4 %5 %6 %7 %8 %9
goto :eof
This makes batch file run itself as elevated user. It adds two parameters to the privileged code:
word payload, to indicate this is payload call, i.e. already elevated. Otherwise
it would just open new processes over and over.
directory path where the main script was called. Due to the fact that Windows always
starts elevated cmd.exe in "%windir%\system32", there's no easy way of knowing what
the original path was (and retaining ability to copy your script around without
touching code)
Note: Unfortunately, for some reason shift does not work for %*, so if you need
to pass actual arguments on, you will have to resort to the ugly notation I used
in the example (%1 %2 %3 %4 %5 %6 %7 %8 %9), which also brings in the limit of
maximum of 9 arguments
To prevent the script from failing when the script file resides on a non system drive (c:) and in a directory with spaces.
Batch_Script_Run_As_Admin.cmd
#echo off
if _%1_==_payload_ goto :payload
:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) >> "%vbs%"
echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
:payload
::ENTER YOUR CODE BELOW::
::END OF YOUR CODE::
echo.
echo...Script Complete....
echo.
pause
You can use a shortcut that links to the batch file. Just go into properties for the shortcut and select advanced, then "run as administrator".
Then just make the batch file hidden, and run the shortcut.
This way, you can even set your own icon for the shortcut.
This Works for me in Windows 7 to 10 with parameters, when kick starting app or file from anywhere (including browser) and also when accessing file from anywhere. Replace (YOUR BATCH SCRIPT HERE anchor) with your code. This solution May Help :)
#echo off
call :isAdmin
if %errorlevel% == 0 (
goto :run
) else (
echo Requesting administrative privileges...
goto :UACPrompt
)
exit /b
:isAdmin
fsutil dirty query %systemdrive% >nul
exit /b
:run
<YOUR BATCH SCRIPT HERE>
exit /b
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %~1", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
Maybe something like this:
if "%~s0"=="%~s1" ( cd %~sp1 & shift ) else (
echo CreateObject^("Shell.Application"^).ShellExecute "%~s0","%~0 %*","","runas",1 >"%tmp%%~n0.vbs" & "%tmp%%~n0.vbs" & del /q "%tmp%%~n0.vbs" & goto :eof
)
As user2549366 suggested before, "You can use a shortcut that links to the batch file." but in the Properties->Compatibility tab of the shortcut, run as administrator may be disabled.
So instead You just right click on your "file.bat - shortcut" then go to ->Properties->Shortcut tab -> Advanced and there you can click Run as administrator. After that, You can execute the shortcut.
Here's a more simple version of essentially the same file.
#echo off
break off
title C:\Windows\system32\cmd.exe
cls
:cmd
set /p cmd=C:\Enter Command:
%cmd%
echo.
goto cmd
Make a text using notepad or any text editor of you choice. Open notepad, write this short command "cmd.exe" without the quote aand save it as cmd.bat.
Click cmd.bat and choose "run as administrator".

Resources