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 }

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?


Avatar of undefined
Last Comment
String :-)

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
String :-)

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck