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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

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

#1 Post by alan_b » 14 Jan 2012 02:03

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

aGerman
Expert
Posts: 3063
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 14 Jan 2012 07:19

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

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

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

#3 Post by alan_b » 14 Jan 2012 16:53

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

jaheaga
Posts: 5
Joined: 04 Oct 2011 14:01

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

#4 Post by jaheaga » 06 Mar 2012 13:43

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

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

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

#5 Post by Liviu » 06 Mar 2012 15:34

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

Post Reply