As part of an installer, I need to run a batch file from ANT. If I run cmd.exe as Administrator and run the batch file, all is well since it has the appropriate administrative privileges. When the batch file is executed from ant, it fails, the same way it does if I were to run the batch file without administrative privileges. My question is, how can I run this batch file in Administrative mode from my ANT script?
<exec executable="cmd.exe" output="dir.txt" dir="c:/bin/">
<arg line="/c service.bat install"/>
</exec>
I've stumbled upon a similar problem. The solution was to use PsExec as the executable, and use it to call the batch file. PsExec is a powerful replacement for Windows' runas command.
You could try the Script Elevation PowerToy. It adds an elevate command that can be used to elevate privileges on the command line.
Turning off UAC seems to be the only option to allow this ant task to execute.
I tried making a shortcut to the batch file, and running that, since shortcuts can be set to 'run as administrator'. No luck there either as I get the prompt, but my batch file still fails out.
[http://www.mydigitallife.info/2007/02/17/how-to-open-elevated-command-prompt-with-administrator-privileges-in-windows-vista/][1]
by using the elevate command prompt we can do this.
In Ant task we can use exec task to run any executable file.Like that we can also use command prompt with exec tas
In the above code snippet elevate.cmd source can be downloaded from here
to run this code correctly we need to put the two files elevate.vbs, elevate.cmd in the same directory and should be available in appropriate location which is define in the property admincmd.
here is the reference link of the original post
At least XP has a
runas
command which you can try to use, something like:When invoked, it will ask for password on console.
UPDATE: half of year later, I have upgraded to Windows 7. Here runas cannot be used for privilege elevation, but Aaron Margosis has a solution:
Which perhaps could be embedded in the installer if needed. For end users though, the Script Elevation Power Toys is more convienent, as suggested by another answer.