Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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" } 
share|improve this answer
    
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. –  gregs Mar 2 '12 at 16:34
    
@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. –  Andy Arismendi Mar 2 '12 at 17:16
    
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. –  gregs Mar 2 '12 at 18:24
1  
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. –  gregs Mar 2 '12 at 19:41
1  
@gregs Right, ArgumentList is an array. –  Andy Arismendi Mar 2 '12 at 19:58
show 1 more comment
$sb = ScriptBlock::Create("$command")
Invoke-Command -ScriptBlock $sb

This should work and avoid misleading the beginners.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.