running elevated cmd in batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
manik
Posts: 3
Joined: 15 Oct 2014 06:26

running elevated cmd in batch script

#1 Post by manik » 15 Oct 2014 06:53

Hi All,

I am writing a dos batch file for windows7 and it has many commands which need elevated access to run. For elevation I am using elevate.exe. I am doing like below -

set Elevation=elevate -wait cmd.exe /c
%Elevation% "cd /d %CD% && xcopy /E /Y ...."
%Elevation% "cd /d %CD% && command 2 .."
%Elevation% "cd /d %CD% && command 3 .."
%Elevation% "cd /d %CD% && command 4 .."
.....

My script calls elevate for cmd and then "cd /d %CD% for each command as elevate changes the working path to /system32 I am forced to change the directory every time I run elevate. It also prompts user for "UAC" dialog for each elevate command, so for ten such command UAC prompts will come 10 times :cry:. Commands I am using are mix of DOS command(like xcopy, diskpart etc) and some other 3rd party executable. So there is two problem for which I need some help-

1. How to call elevate for cmd.exe once and run rest of the command in same elevated console.
2. How to change working directory once instead with every command. I think solution to first one will take care of this also.

Any help is most welcome.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: running elevated cmd in batch script

#2 Post by ShadowThief » 15 Oct 2014 10:16

You could run the script as an Administrator and eliminate the need for elevate.exe altogether.

manik
Posts: 3
Joined: 15 Oct 2014 06:26

Re: running elevated cmd in batch script

#3 Post by manik » 15 Oct 2014 11:57

Because of some internal requirement option to run this script as administrator is not available.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: running elevated cmd in batch script

#4 Post by Squashman » 15 Oct 2014 12:00

Well then wouldn't the easiest solution be to use the elevate command to run the batch file instead of using elevate in the batch file.

manik
Posts: 3
Joined: 15 Oct 2014 06:26

Re: running elevated cmd in batch script

#5 Post by manik » 15 Oct 2014 12:08

@Squashman, can you please elaborate little more. Do you mean to run this batch script elevated from another batch script. Because of some restriction due to legacy code it is not allowed to add new script in that source code.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: running elevated cmd in batch script

#6 Post by Squashman » 15 Oct 2014 12:28

manik wrote:@Squashman, can you please elaborate little more. Do you mean to run this batch script elevated from another batch script. Because of some restriction due to legacy code it is not allowed to add new script in that source code.

Not really following the restrictions you have. You can't add a script but you can edit a script. Not sure that makes any sense. How can you possibly restrict that and still be able to use this third party utility. If your system admin won't allow you to right click and runas admin then I would assume they would not want you to use this ELEVATE command either.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: running elevated cmd in batch script

#7 Post by Squashman » 15 Oct 2014 12:44

If I am understanding you correctly this should do what you want it to do. Basically the batch file is running itself, but the 2nd time it runs itself it using the Elevate command and passing the current directory to the batch file. The PUSHD command will set the current directory. I don't know what version of elevate you are using so just test this with a few xcopy commands after the REM statement.

Code: Select all

@echo off
IF "%1"=="" elevate.exe "%~0" "%CD%" &exit /b
IF NOT "%1"=="" PUSHD "%~dp1"

REM put the rest of your commands below without the Elevate command before it.

Post Reply