Is there a way to make an ANT task run as Administ

2020-06-16 09:01发布

问题:

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>

回答1:

At least XP has a runas command which you can try to use, something like:

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

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:

// 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");
}

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.



回答2:

You could try the Script Elevation PowerToy. It adds an elevate command that can be used to elevate privileges on the command line.



回答3:

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.



回答4:

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]



回答5:

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

<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 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