View unanswered posts | View active topics It is currently 20 Oct 2014 15:09



Post new topic Reply to topic  [ 5 posts ] 
How to "RUN AS ADMINISTRATOR" from a *.bat script. 
Author Message
Expert

Joined: 04 Oct 2008 09:49
Posts: 357
Post How to "RUN AS ADMINISTRATOR" from a *.bat script.
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


14 Jan 2012 02:03
Profile
Expert

Joined: 22 Jan 2010 18:01
Posts: 1800
Location: Germany
Post Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.
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


14 Jan 2012 07:19
Profile
Expert

Joined: 04 Oct 2008 09:49
Posts: 357
Post Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.
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


14 Jan 2012 16:53
Profile

Joined: 04 Oct 2011 14:01
Posts: 5
Post Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.
You can do it like this
Code:
@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


06 Mar 2012 13:43
Profile
Expert

Joined: 13 Jan 2012 21:24
Posts: 441
Post Re: How to "RUN AS ADMINISTRATOR" from a *.bat script.
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:
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


06 Mar 2012 15:34
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


Who is online

Users browsing this forum: dchall8, facescreen, Yahoo [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.