Get-service | Start-process

Hi,

I’m a beginner with PS and couldn’t find on the network the information:
I want to start process on my computer only after a specific service is on “Running” status in another computer in the same network.
i believe that it’s a kind of a loop that will stop and then will run the start-process cmdlet when the service is running. 1 second interval between every attempt required.

 

Thanks for your help

Dear,

perhaps you can do something with get-help Get-Service -Full or Get-service -Examples in order to see what options and switches you can use.

good luck.

 

Thanks for your replay.

since “If” and “Else” must be involved it’s not the exact examples i need.

So you’ll need Invoke-Command for the remote machine call to contain the Get-Service call… but you can in theory just wait for it to be running.

while ((Invoke-Command -ComputerName $RemotePCName -ScriptBlock { Get-Service -Name $using:ServiceName }).Status -ne "Running") {
    # Pause before next check
    Start-Sleep -Seconds 5 
}

Start-Process -FilePath $Process

adding some perf improvements to @ta11ow 's reply.

$Session = New-PSSession -ComputerName $RemotePCName
while ((Invoke-Command -Session $Session -ScriptBlock { Get-Service -Name $using:ServiceName }).Status -ne "Running") {
    # Pause before next check
    Start-Sleep -Seconds 5 
}

about that:

$using:ServiceName

should all of it be replaced with the service name?

$Using notation is a feature made available from PS 3.0. This will help to use local variables in a remote session. If I create a variable $foo = 1
I will access it like $using:foo in a remote session.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-5.1

Correct me if i’m wrong, PSsession or invoke-command will not work if WinRM is not enabled at the RemotePC.
When i’m writing in my computer Get-Service -ComputerName Computer1 -Name ServiceName it provides the requested information.
all i need is that when the status is running, go to the next step which is start-process

Guys, thanks for your help, base on your answer and my logic i’ve found that the next works for me:

while ((Get-Service -ComputerName $ComputerName -ServiceName $ServiceName).Status -ne "Running") {
    # Pause before next check
    Start-Sleep -Seconds 3 
}

Start-Process $process

last thing i need and will be very very appriciated:

  1. maximum 3 minutes of trying
  2. at the time loop runs - display a note in the form
  3. if 3 minutes left and the condition didn’t happen - stop script and display a note.

guys, i really appreciate your help

yup, PSSessions can be created only via PowerShell remoting which is using winrm (till 5.1) and of course above solution is best if you have winrm contrains.