String :-)
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\SplunkUniversalForwa rder\etc\s ystem\loca l\" -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
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\SplunkUniversalForwa
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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