Posted by: deepcani | June 2, 2010

Calling PsExec from Ant Script

Part of the work I’m doing has to do with launching Amazon instances on EC2. Once the instance is running and is ready to be used we install various software on it remotely, via ant script. And, like many other folks out there we rely on PsExec, a tool formerly from Sysinternals (and now from Microsoft).

If you google for ‘psexec from ant’ you’ll find quite a few unresolved issues and disappointed forum posts. People complain that psExec hangs, and that they cannot see the output of the tool when it is called from ant… Below I describe a workaround that worked for me. The workaround wraps PsExec into a batch file, and starts the tool in a separate console window. This way the tool is launched from a console window environment, and not really from ant.

Sample batch file (runPsExec.bat):

start "" /wait %pstools.home%/psexec %*
exit %ERRORLEVEL%

And here is how to use it in ant:

<exec executable="cmd.exe" failonerror="true" >
  <arg value="/C"/>
  <arg value="${basedir}/runPsExec.bat"/>
  <arg line="\\remoteHost -u xxxx -p xxxx ipconfig /all"/>
</exec>

The second line in runPsExec.bat script is needed, because otherwise the error code is not propagated from the shell to your ant script.

Advertisements

Responses

  1. This is great.
    Although this does take away the visibility of the output of the command being executed.

  2. Dustin, maybe you could output the command itself in the tile of a new window. E.g. modify the batch script: start “Place Command Here” /wait %pstools.home%/psexec %*

  3. Thanks, this is a very useful post indeed! Saved me a lot of time.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: