Luis Diaz
asked on
Windows batch: run as administrator
Hello experts,
I have the following .bat.
I would like to add a previous line to force the run as administrator.
How should I proceed?
Thank you for your help.
I have the following .bat.
"C:\API\file.bat" NO_THRESHOLD
I would like to add a previous line to force the run as administrator.
How should I proceed?
Thank you for your help.
ASKER
I tested the following without success:
NOT_THRESHOLD is a variable defined in .bat
runas /user:administrator "C:\API\file.bat" NO_THRESHOLD
NOT_THRESHOLD is a variable defined in .bat
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What did you see the command runs, but no rights?
What version of operating system do you have?
What version of operating system do you have?
ASKER
Thank you Bill, and the command should be saved in .bat or .ps1?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use this piece of code in batch to run as admin :
@echo off
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
REM --> Check for permissions
Reg query "HKU\S-1-5-19\Environment" >nul 2>&1
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo.
ECHO **************************************
ECHO Running Admin shell... Please wait...
ECHO **************************************
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
::::::::::::::::::::::::::::
:: START ::
::::::::::::::::::::::::::::
"C:\API\file.bat" NO_THRESHOLD
ASKER
Tested and it works.
Thank you for your help.
Thank you for your help.
You can try to use the command RUNAS
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc771525(v%3Dws.11)