Saturday 6 May 2017

Launch Batch Script to Prompt for Administrator Privileges in a GUI


Launch Batch Scripts as Administrator (with GUI UAC prompt)

Imagine you write a batch script(.bat) and you converted it to an executable file (.exe), then you try to run it, it says access denied.
You could add the runas command to your batch script and have to type the admin password at the prompt.
OR 
You should put into your batch script some additional batch code (as shown below) that launches the batch script or created executable (.exe) file as an administrator GUI UAC prompt, where we can select either yes or no options, rather than have to type in the admin password on the prompt.
Thank you Eneergehttps://sites.google.com/site/eneerge/scripts/batchgotadmin

Solved, tested and it works, find script below, copy and paste, then run.
@echo off
REM Launch Batch Scripts as Administrator (with GUI UAC prompt)
:: BatchGotAdmin
:-------------------------------------
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 )
: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"
:--------------------------------------
REM <YOUR BATCH SCRIPT HERE>


Stackover Source: http://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file

2 comments :

  1. Wao, What a great Post it is. Just amazing to know these all things. We have to make the blogging easier with these tricks. Thanks a lot sir. Visit here PayPal gift card code generator

    ReplyDelete

  2. This is very informative and interesting for those who are interested in blogging field.
    Click this link for interacting Hd wallpapers for android

    ReplyDelete