invoke-command with computername or within pssession - what's the difference?

Hi all,

I want to run a utility (exe) with parameters on a remote machine (server). The utility has to run on the server because of its architecture (local installation/database etc.).
I tried to do this with invoke-command. When I run invoke-command within a pssession, it works. When I run invoke-command with the -computername parameter, I do not get any result. In the PowerShell eventlog, I can see the command executes the way I want it, but nothing happens.

I’m assuming the exe runs interactively, but not without interaction. Is there any way to troubleshoot this, and find out what is causing the exe to fail? Or, are there any other methods I can use to run exe’s remotely? What is the difference between using invoke-command within a remoting session and using invoke-command with the computername parameter?

Extra information:

I use the following command:

Invoke-Command -ComputerName MyServer -ArgumentList arg1,arg2 -ScriptBlock {param(arg1,arg2);Start-Process -FilePath ‘D:\Program Files\MyApp\MyExe.exe’ -ArgumentList “arg1 arg2” -WorkingDirectory ‘D:\Program Files\MyApp\MyExe.exe’ -RedirectStandardOutput ‘D:\Logs\Out.log’ -RedirectStandardError ‘D:\Logs\Error.log’}

The above command works perfectly when I first start a pssession and run the command without the -ComputerName parameter.

I have a backup plan, being the task scheduler, but I would rather do it with PowerShell.

Any ideas?

Thanks in advance!

Can you show us what commands you’re using when it’s working? What you’ve described so far sounds a bit off, but it could just be a terminology thing.

Check out “Secrets of PowerShell Remoting” (free ebook), as it explains this all in a bit more detail.

There shouldn’t be any difference in using -Session versus -ComputerName; both work exactly the same. With -ComputerName, you’re spinning up a session and then releasing it. What might be happening in your case, because you’re running a non-PowerShell executable, is in how the remote PowerShell (WsmProvHost.exe) is shelling out to Cmd.exe in order to run the command. It’s possible that PowerShell thinks the command has completed, and terminates the session, before the command has finished returning all its output or running. When you use -Session, the session remains open, possibly giving the command enough time to execute.

If Don’s idea turns out to be the problem, you should be able to solve that by adding the -Wait switch to your Start-Process command.

Thanks a lot for the quick responses. I just tried the -Wait switch, but this gives the same result.

The exe is called deftable.exe and is a utility of Control-M Workload Automation (BMC Software). This software has several utilities to scripts actions about the management of the workload automation product. We used to perform this actions via batch scripting, but I’m trying to rewrite this stuff into powershell to improve our processes. It is my intention to create modules to automate this management stuff, but using pssession within modules didn’t seem like a good idea to me.

The exact command I use is:
Start-Process -FilePath “D:\Program Files\BMC Software\Control-M EM 7.0.00\Default\bin\deftable.exe” -ArgumentList “-HOST MyServer -SRC_FILE D:\Files\MyXml.xml -USERNAME myuser -PASSWORD mypassword” -WorkingDirectory “D:\Program Files\BMC Software\Control-M EM 7.0.00\Default\bin” -RedirectStandardOutput “D:\Logs\Output.log” -RedirectStandardError “D:\Logs\Error.log”

I tried this several ways, all with the same result. To troubleshoot, I’ve created the most simple scenario, which is creating a ps1-file. This ps1-file contains the above command, and nothing else.
When I use, interactively, the following commands:

New-PSSession -ComputerName MyServer
Enter-PSSession -ComputerName MyServer

…it works !

When I use:

Invoke-Command -ComputerName MyServer -FilePath C:\Temp\MyPs1.ps1.

… both D:\Logs\Output.log and D:\Logs\Error.log are created, but are empty. The exe launches (I’ve seen this via Process Monitor), but seems to be doing nothing.

D:\Files\MyPs1.ps1 is on the Server, C:\Temp\MyPs1.ps1 is on my client. Of course, the ps1-files are identical. It is driving me crazy. I’m assuming this is not a error in my scripting because I tried already so many things, and all results gave the same outcome. It looked to me as if the executable does not work when called in a certain way. That is why I assumed there was a difference between using the -computername parameter and the interactive remoting via a pssession. I already looked into Secrets of PowerShell remoting, but I will delve in deeper.

Anyways, any more ideas are more than welcome!!! If additional information is required, let me know.


Well, it seems I have a possible solution.

Don’s excellent explanation and suggestion about the timing made me experiment further.

So the -wait didn’t work, possibly because the executable already gives a result back to powershell while there are still things that need to finish.
So I tried putting a start-sleep after the start-process. So invoke-command does the start-process and then the start-sleep, and guess what… It works!!!

I’m going to experiment further, but it seems I have a solution.

Thanks Don and Dave, you guys rock !!!