mr_evans2u
mr_evans2u

Reputation: 149

Issues running script to run remote .exe

If I run the below script on the computer it works as expected and installs.

Start-Process "C:\Test_Software\TestConfigApp" " -s"

But I need to run this remotely. So this is the code I have but it is not working. $Computer is defined and is the correct device. What am I doing wrong? I have other Scripts that I run remotely to this device so I know I am getting to it correctly and not getting blocked.

Invoke-Command -ComputerName $Computer -ScriptBlock {Start-Process "C:\Test_Software\TestConfigApp" " -s"}

Upvotes: 1

Views: 524

Answers (1)

mklement0
mklement0

Reputation: 437698

Start-Process launches a process asynchronously by default. That is, control is instantly returned to PowerShell, even if the launched process is still running.

  • In a local invocation, the launched process lives on independently of the calling process.

  • In a remote invocation, the remote session ending automatically terminates processes launched via Start-Process.

This asymmetry exists for technical reasons and is explained in GitHub issue #16001.


This means that your C:\Test_Software\TestConfigApp process most likely never gets a chance to run to completion, given that your remote session ends right after the Start-Process call.

To ensure that it does, add -Wait to the Start-Process call:

Invoke-Command -ComputerName $Computer -ScriptBlock {
  Start-Process -Wait 'C:\Test_Software\TestConfigApp' '-s'
}

Note:

  • Remoting commands run in an invisible window station, in which some GUI applications may refuse to run. When that happens, you'll need to look for a logging mechanism that can help you troubleshoot; perhaps the remote machine's event log provides clues.

  • If, by contrast, C:\Test_Software\TestConfigApp happens to be a console application, you can achieve the same effect - synchronous execution - with direct invocation (which is generally the right way to invoke console applications - see this answer):

Invoke-Command -ComputerName $Computer -ScriptBlock {
  # Only if the application is a *console* application.
  C:\Test_Software\TestConfigApp -s
}

Upvotes: 2

Related Questions