8

I'm working on creating a single command that will run mulitple things on the command line of another machine. Here is what I'm looking to do.

  • Use psexec to access remote machine
  • travel to proper directory and file
  • execute ant task
  • exit cmd
  • run together in one line

I can run the below command from Run to complete what I need accomplished but can't seem to get the format correct for psexec to understand it.

cmd /K cd /d D:\directory & ant & exit

I've tried appling this to the psexec example below:

psexec \\machine cmd /K cd /d D:\directory & ant & exit 

When executing this it will activate the command line and travel to D:\directory but won't execute the remaining commands. Adding "" just creates more issues.

Can anyone guide me to the correct format? Or something other than psexec I can use to complete this (free options only)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Steve Miskiewicz
  • 1,114
  • 1
  • 10
  • 23

5 Answers5

10

Figured it out finally after some more internet searching and trial and error. psexec needs /c to run multiple commands, but that syntax doesn't work with the setup I wrote above. I've gotten the below command to run what I need.

psexec \\machine cmd /c (^d:^ ^& cd directory^ ^& ant^) 

I don't need to exit because psexec will exit itself upon completion. You can also use && to require success to continue on to the next command. Found this forum helpful

http://forum.sysinternals.com/psexec_topic318.html

And this for running psexec commands

http://ss64.com/nt/psexec.html

andronoid
  • 25
  • 6
Steve Miskiewicz
  • 1,114
  • 1
  • 10
  • 23
  • 1
    Can you please explain to me when exactly to put `^`, I can't figure it out with this code. – IGRACH Nov 23 '16 at 12:38
2

This works:

psexec \ComputerName cmd /c "echo hey1 & echo hey2"

TByte
  • 121
  • 1
  • 5
1

For simple cases I use:

PsExec \\machine <options> CMD /C "command_1 & command_2 & ... & command_N"

For more complex cases, using a batch file with PsExec's -c switch may be more suitable:

The -c switch directs PsExec to copy the specified executable to the remote system for execution and delete the executable from the remote system when the program has finished running.

PsExec \\machine <options> -c PSEXEC_COMMANDS.cmd <arguments>
andronoid
  • 25
  • 6
0

I always use like this way :) and works properly

psexec \\COMPUTER -e cmd /c (COMMAND1 ^& COMMAND2 ^& COMMAND3)
serdar
  • 147
  • 6
0

Since you asked about other options and this has tag configuration managment-- I guess you may be interested in Jenkins (or Hudson). It provide very good way of creating master-slave mechanism which may help in simplifying the build process.

Jayan
  • 18,003
  • 15
  • 89
  • 143