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:
- Win32_Process.Create()
- Invoke-Command
- Start-Process
- [diagnostics.process]::start
- 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.
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" }
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)
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')
$sb = [scriptblock]::create($command)
$sb = ScriptBlock::Create("$command")
Invoke-Command -ScriptBlock $sb
This should work and avoid misleading the beginners.