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 }

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?

String :-)Windows EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

No real need to use PS Remoting here (and if you explicitly create a new remote session, you should remove it when you're done, otherwise it will remain open on the remote machine).
Try it like this:
$serviceName = 'SplunkForwarder'
$serviceTimeout = 300	## seconds
Get-Content .\servers.txt | ForEach-Object {
	$computerName = $_
	Write-Host "Processing $($_) ..."
	Try {
		$service = Get-Service -Name $serviceName -ComputerName $computerName -ErrorAction Stop
		$stopTime = (Get-Date).AddSeconds($serviceTimeout)
		$service | Stop-Service -ErrorAction Stop
		Do {
			Start-Sleep -Seconds 1
		} Until (($service.Status -eq 'Stopped') -or ((Get-Date) -gt $stopTime))
		If ($service.Status -ne 'Stopped') {
			Throw "$($computerName): Service '$(ServiceName)' did not stop after $($serviceTimeout) seconds!"
		Copy-Item -Path ".\Fix\*" -Destination "\\$($computerName)\C$\Program Files\SplunkUniversalForwarder\etc\system\local\" -Force -ErrorAction Stop
		$service | Start-Service -ErrorAction Stop
	} Catch {

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
String :-)Windows EngineerAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.