Author Posts

February 16, 2018 at 1:05 pm

I use this code to start an executable on multiple computers.

Invoke-Command -ComputerName Comp1, Comp2, Comp3 -ScriptBlock {
D:\XXXXX.exe }

The script keeps running until the executable is stopped on all machines.
Is there a way to exit the script without terminating the executable?

February 16, 2018 at 1:11 pm

You could look into using jobs https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Core/Invoke-Command?view=powershell-5.1

https://www.howtogeek.com/138856/geek-school-learn-how-to-use-jobs-in-powershell/

From the examples:

$s = New-PSSession -ComputerName Server01, Server02
Invoke-Command -Session $s -ScriptBlock {Get-EventLog system} -AsJob

Id Name State HasMoreData Location Command
— —- —– —– ———– —————
1 Job1 Running True Server01,Server02 Get-EventLog system

$j = Get-Job

$j | Format-List -Property *

HasMoreData : True
StatusMessage :
Location : Server01,Server02
Command : Get-EventLog system
JobStateInfo : Running
Finished : System.Threading.ManualResetEvent
InstanceId : e124bb59-8cb2-498b-a0d2-2e07d4e030ca
Id : 1
Name : Job1
ChildJobs : {Job2, Job3}
Output : {}
Error : {}
Progress : {}
Verbose : {}
Debug : {}
Warning : {}
StateChanged :

$results = $j | Receive-Job