Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Execute remote exe with command line arguments on remote computer

Tags:

I've searched all over and tried different variations of commands, but I am still not there yet.

My goal is to run an exe that already resides on a remote machine and pass in command line arguments. I've tried invoke-command, but I can't seem to get my syntax to recognize the arguments.

Methods tried:

  1. Win32_Process.Create()
  2. Invoke-Command
  3. Start-Process
  4. [diagnostics.process]::start
  5. Invoke-WmiMethod

From my tests, the closest I can get is with the following command:

$command = "program.exe -r param" Invoke-Command -ComputerName $server -ScriptBlock {$command} 

The command completes without error or return code, but on the remote machine, it did not run with arguments. I've played around with single/double quotes to see if any change, but none.

like image 886
gregs Avatar asked Mar 02 '12 15:03

gregs


2 Answers

Did you try using the -ArgumentList parameter:

invoke-command -ComputerName studio -ScriptBlock { param ( $myarg ) ping.exe $myarg } -ArgumentList localhost    

http://technet.microsoft.com/en-us/library/dd347578.aspx

An example of invoking a program that is not in the path and has a space in it's folder path:

invoke-command -ComputerName Computer1 -ScriptBlock { param ($myarg) & 'C:\Program Files\program.exe' -something $myarg } -ArgumentList "myArgValue" 

If the value of the argument is static you can just provide it in the script block like this:

invoke-command -ComputerName Computer1 -ScriptBlock { & 'C:\Program Files\program.exe' -something "myArgValue" }  
like image 194
Andy Arismendi Avatar answered Oct 11 '22 15:10

Andy Arismendi


Are you trying to pass the command line arguments to the program AS you launch it? I am working on something right now that does exactly this, and it was a lot simpler than I thought. If I go into the command line, and type

C:\folder\app.exe/xC:\folder\file.txt 

then my application launches, and creates a file in the specified directory with the specified name.

I wanted to do this through a Powershell script on a remote machine, and figured out that all I needed to do was put

$s = New-PSSession -computername NAME -credential LOGIN     Invoke-Command -session $s -scriptblock {C:\folder\app.exe /xC:\folder\file.txt} Remove-PSSession $s 

(I have a bunch more similar commands inside the session, this is just the minimum it requires to run) notice the space between the executable, and the command line arguments. It works for me, but I am not sure exactly how your application works, or if that is even how you pass arguments to it.

*I can also have my application push the file back to my own local computer by changing the script-block to

C:\folder\app.exe /x"\\LocalPC\DATA (C)\localfolder\localfile.txt" 

You need the quotes if your file-path has a space in it.

EDIT: actually, this brought up some silly problems with Powershell launching the application as a service or something, so I did some searching, and figured out that you can call CMD to execute commands for you on the remote computer. This way, the command is carried out EXACTLY as if you had just typed it into a CMD window on the remote machine. Put the command in the scriptblock into double quotes, and then put a cmd.exe /C before it. like this:

cmd.exe /C "C:\folder\app.exe/xC:\folder\file.txt" 

this solved all of the problems that I have been having recently.

EDIT EDIT: Had more problems, and found a much better way to do it.

start-process -filepath C:\folder\app.exe -argumentlist "/xC:\folder\file.txt" 

and this doesn't hang up your terminal window waiting for the remote process to end. Just make sure you have a way to terminate the process if it doesn't do that on it's own. (mine doesn't, required the coding of another argument)

like image 26
Kyle Gagnon Avatar answered Oct 11 '22 16:10

Kyle Gagnon