Powershell Execute remote exe with command line arguments on remote computer

181,434

Solution 1

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" } 

Solution 2

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)

Solution 3

I have been trying to achieve this by using 1 row single entry but I ended that Invoke command did not worked successfully because it is killing the process immediately after starting despite it can work if you are entering the session and waiting enough before exiting.

The only working way I could find, for example, to run a command with arguments and space on the remote machine HFVMACHINE1 (running Windows 7) is the following:

([WMICLASS]"\\HFVMACHINE1\ROOT\CIMV2:win32_process").Create('c:\Program Files (x86)\Thinware\vBackup\vBackup.exe -v HFSVR12-WWW')
Share:
181,434
gregs
Author by

gregs

Updated on May 21, 2020

Comments

  • gregs
    gregs about 4 years

    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.

  • gregs
    gregs over 12 years
    Thanks for the quick response. For some reason, when I try it that way, I get it to print out the expanded command that would work if I ran it manually, but it's not actually running it. I had to remove the "&" as it said it was a reserved character for future use.
  • Andy Arismendi
    Andy Arismendi over 12 years
    @gregs I had to enable remoting on my machine to test. I changed the command to a simple ping test sending an argument of localhost. The above succeeding for me.
  • gregs
    gregs over 12 years
    I may have figured out the hold up, but not the solution. This line does work if I use ping.exe and "localhost" as the arg, but I am having to put quotes around my executable path since the path has spaces. When I do that, it simply echos my string in quotes back to me, but I don't think it runs.
  • Andy Arismendi
    Andy Arismendi over 12 years
    @gregs I added an example of how to handle a space in the path.
  • gregs
    gregs over 12 years
    Thanks for the examples. I actually found my problem, it doesn't like it you try to slam 2 arguments into a single variable, I had to end up doing two params $myarg1, $myarg2 so the remote command would honor them both.
  • Andy Arismendi
    Andy Arismendi over 12 years
    @gregs Right, ArgumentList is an array.
  • Maximilian Peters
    Maximilian Peters about 8 years
    Welcome to StackOverflow! Perhaps you could add some explanation, it might be helpful for the person who asked the question.
  • Igor Beaufils
    Igor Beaufils over 7 years
    Best answer yet
  • user2181700
    user2181700 almost 6 years
    I have a bat file with two line of code, The first line lauches the application the second run the command. could you give me an example of how I could get his to run? "C:\Program Files\Number.exe" ^ "C:\TEST\Syntax\Stats.spj" -production silent