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
Powershell

Avatar of undefined
Last Comment
String :-)
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of String :-)
String :-)
Flag of Australia image

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
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo