Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple commands in cmd via psexec

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

like image 439
Steve Miskiewicz Avatar asked Jan 04 '12 19:01

Steve Miskiewicz


People also ask

How do I run multiple commands in PsExec?

Create a script pass the -c option so the script is copied to the remote system. That would force us to run multiple commands, first copying the script to all the machines then running it.

How do you run multiple commands?

Running Multiple Commands as a Single Job Combining the commands – We can use “;“, “&&“, or “||“ to concatenate our commands, depending on the requirement of conditional logic, for example: cmd1; cmd2 && cmd3 || cmd4.

How do I run a command on a remote computer using PsExec?

Open the windows command prompt and switch to the pstools directory, then type psexec and press enter. You should see PsExec return the version and command syntax. In the screenshot below you can see I changed to the “c:\pstools” directory to run the psexec command.


2 Answers

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

like image 193
Steve Miskiewicz Avatar answered Oct 28 '22 06:10

Steve Miskiewicz


This works:

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

like image 22
TByte Avatar answered Oct 28 '22 07:10

TByte