Page 1 of 1

How to "RUN AS ADMINISTRATOR" from a *.bat script.

Posted: 14 Jan 2012 02:03
by alan_b
I have a script which does things that UAC may object to.
It can detect a failure and suggest to the user to "Right Click and Run As Administrator".

Is it possible for the script to invoke itself by some sort of call / run / start / whatever /
and to thus run itself with elevated privileges, and if so how ?

I have tried and my password failed with
RUNAS /USER:Administrator D:\zz.BAT

I think I had success with that in the past, but that may have been whilst Windows login included the BuiltIn Administrator capability.

"Run As Administrator" does its job WITHOUT needing a password, is it a totally different animal from RUNAS ?

I am using Windows 7 Ultimate, but would like my script to be usable on "lower forms of life" which are also subject to UAC

Regards
Alan

Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.

Posted: 14 Jan 2012 07:19
by aGerman
With the RUNAS command you can execute applications in another user account. Also for the user Administrator the UAC is enabled by default. Log in as Administrator and disable the UAC for this account. Now the RUNAS command would have probably the same effect as "Run As Administrator".

Regards
aGerman

Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.

Posted: 14 Jan 2012 16:53
by alan_b
Sorry, I failed to explain my script is for myself and others with different abilities.
I cannot consider disabling Administator passwords or UAC.
Until today I had assumed RUNAS /USER:Administrator had a similar effect to
"Right Click and Run As Administrator".

All I wanted was that my BAT script should launch an executable with identical capability as "Right Click and Run As Administrator",
but that looks beyond reach.

I am now giving up and asking the user to "Right Click and Run As Administrator" when appropriate, as at
viewtopic.php?f=3&t=2797

Regards
Alan

Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.

Posted: 06 Mar 2012 13:43
by jaheaga
You can do it like this

Code: Select all

@echo off
mkdir "%windir%\BatchGotAdmin"
if '%errorlevel%' == '0' (
  rmdir "%windir%\BatchGotAdmin" & goto gotAdmin
) else ( goto UACPrompt )
:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute %0, "", "", "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"
"here you can put some stuff you want to execute"


The code detects if you got admin privileges, if not run the command as administrator, of course the user have to accept the pop up window.

Actually I use it to disable the user account control on a machine with Vista or 7

Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.

Posted: 06 Mar 2012 15:34
by Liviu
alan_b wrote:"Run As Administrator" does its job WITHOUT needing a password, is it a totally different animal from RUNAS ?
Yes, indeed. As confusing as MS chose to make UAC be, "run as administrator" does not mean "run under the administrator user account", but rather "run as the current user, but using its elevated token".

You may try something like...

Code: Select all

psexec -accepteula -h cmd /c "%~0" 
using PsExec from PsTools http://technet.microsoft.com/en-us/sysinternals/bb897553.

Note that the "-h" switch is not documented on the PsExec page, yet shows up in its /? help:
'psexec /?' wrote: -h If the target system is Vista or higher, has (sic) the process
run with the account's elevated token, if available.

Liviu