Is there a way to make the ANT task run as an administrator in Windows Vista?

As part of the installer, I need to run the batch file from ANT. If I run cmd.exe as an administrator and run a batch file, all is well, since it has the appropriate administrative privileges. When the batch file is executed from ant, it fails, just as if I were running 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> 
+7
java windows-vista ant
source share
5 answers

Disabling UAC seems to be the only option to complete this ant task.

I tried to make a shortcut to the batch file and run it, since the shortcuts can be set as "run as administrator". I was not lucky since I received an invitation, but my batch file still does not work.

[ http://www.mydigitallife.info/2007/02/17/how-to-open-elevated-command-prompt-with-administrator-privileges-in-windows-vista/†[1]

+1
source share

At least XP has a runas command that you can try using, for example:

 runas /u:%COMPUTERNAME%\Administrator "cmd /c service.bat install" 

When called, it asks for a password on the console.

UPDATE: six months later I upgraded to Windows 7. Here, runas cannot be used to elevate privileges, but Aaron Margosis solution:

 // elevate.js -- runs target command line elevated if (WScript.Arguments.Length >= 1) { Application = WScript.Arguments(0); Arguments = ""; for (Index = 1; Index < WScript.Arguments.Length; Index += 1) { if (Index > 1) { Arguments += " "; } Arguments += WScript.Arguments(Index); } new ActiveXObject("Shell.Application").ShellExecute(Application, Arguments, "", "runas"); } else { WScript.Echo("Usage:"); WScript.Echo("elevate Application Arguments"); } 

Perhaps it can be built into the installer, if necessary. However, for end users, Script Elevation Power Toys is more compelling, as suggested by another answer.

+3
source share

You can try Script Elevation PowerToy . He adds the elevate command, which can be used to elevate privileges on the command line.

+2
source share

I came across a similar problem. The solution was to use PsExec as an executable file and use it to invoke the batch file. PsExec is a powerful replacement for the Windows command line.

+2
source share

using the elevate command, we can do this.

In the Ant task, we can use the exec task to run any executable file. Similar to the fact that we can also use the command line with exec tas

 <property name="admincmd" value="./elevate.cmd" /> <property name="server.location" location="c:/Apache/tomcat/bin" /> <exec executable="${admincmd}" failonerror="false"> <arg value="cmd" /> <arg value="/k" /> <arg value="${server.location}/service" /> <arg value="install" /> </exec> 

In the code snippet above, from bottom to top. cmd source can be downloaded here

in order to run this code correctly, we need to place the two elevate.vbs, elevate.cmd files in the same directory and should be available in the appropriate location, which is defined in the admincmd property.

here is a link to the link to the original message

-2
source share

All Articles