Link to home
Start Free TrialLog in
Avatar of String :-)
String :-)Flag for Australia

asked on

Powershell - How to check service is stopped before I copy the files in a session

Hey all,
Im a newbie with Powershell but giving it a go.
I put together this script to stop a service, copy some files and start the service again on remote computers.

foreach ($server in (Get-Content .\servers.txt)){
$session = New-PSSession $server
Invoke-Command -Session $session -ScriptBlock { Get-Service SplunkForwarder | Stop-Service }
Copy-Item ".\Fix\*" -Destination "C:\Program Files\SplunkUniversalForwarder\etc\system\local\" -Force -ToSession $session
Invoke-Command -Session $session -ScriptBlock { Get-Service SplunkForwarder | Start-Service }
}

Question:
The service I am stopping takes a while to stop, is there a way to add a wait and check service is stopped before I copy the files?

Cheers
String
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of String :-)

ASKER

Thanks again for your amazing help oBdA.
Script worked perfectly :-)

I see why I didnt need to use PS-Remoting so thanks for that tip.

I really like the use of the try, catch throw and will to incorporate this into my next one.
Have a great weekend buddy
String