I know there is a quicker way of doing this but I just don't know how to approach it. I also need it to work in PowerShell V2 as I still have Windows 2003 servers I need to handle with this.
Basically this would stop services and then check to see if those services were stopped. I have a location to define them earlier in the script. I am using this to deploy code to servers so depending on the code from Development I may need to stop 1 to 4 services and looking to see if there is a way that I can define the services and not have to comment out code below if I only use 2 services as opposed to four.
#Turn off Services
stop-service $Service1 -force
# stop-service $Service2 -force
# stop-service $Service3 -force
# stop-service $Service4 -force
$VerifyServiceStopped1 = Get-Service $Service1 | Where-Object {$_.status -eq "Stopped"} | select -last 1
# $VerifyServiceStopped2 = Get-Service $Service2 | Where-Object {$_.status -eq "Stopped"} | select -last 1
# $VerifyServiceStopped3 = Get-Service $Service3 | Where-Object {$_.status -eq "Stopped"} | select -last 1
# $VerifyServiceStopped4 = Get-Service $Service4 | Where-Object {$_.status -eq "Stopped"} | select -last 1
if ($VerifyServiceStopped1) {Write-Host $Service1' Stop = Pass (0)'} else {Write-Host $Service1' Stop = Fail (1000)'; Exit '1000'}
# if ($VerifyServiceStopped2) {Write-Host $Service2' Stop = Pass (0)'} else {Write-Host $Service2' Stop = Fail (1001)'; Exit '1001'}
# if ($VerifyServiceStopped3) {Write-Host $Service3' Stop = Pass (0)'} else {Write-Host $Service3' Stop = Fail (1002)'; Exit '1002'}
# if ($VerifyServiceStopped4) {Write-Host $Service4' Stop = Pass (0)'} else {Write-Host $Service4' Stop = Fail (1003)'; Exit '1003'}
Any suggestions?
Thanks
Dwight