Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Windows batch: run as administrator

Hello experts,

I have the following .bat.

"C:\API\file.bat" NO_THRESHOLD

Open in new window


I would like to add a previous line to force the run as administrator.

How should I proceed?

Thank you for your help.
Avatar of Alexey Komarov
Alexey Komarov
Flag of Russian Federation image

Avatar of Luis Diaz

ASKER

I tested the following without success:

runas /user:administrator "C:\API\file.bat" NO_THRESHOLD

Open in new window


NOT_THRESHOLD is a variable defined in .bat
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What did you see the command runs, but no rights?
What version of operating system do you have?
Thank you Bill, and the command should be saved in .bat or .ps1?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window

Tested and it works.

Thank you for your help.