Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 369
Edward Avatar asked Apr 23 '09 19:04

Edward


1 Answers

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.

like image 53
Laurynas Biveinis Avatar answered Oct 01 '22 10:10

Laurynas Biveinis