I have to stop SQLSERVER service on multiple servers and in the same time check state to be sure they are stopped. Now my code looks like this:
$vmlist = Get-Content C:\xx.txt
foreach($VM in $VMlist)
{
Get-Service -ComputerName $VM | Where-Object -FilterScript {$_.name -match “MSSQLSERVER”} | ForEach-Object {$_.stop()}
Start-Sleep 5
Get-Service -ComputerName $VM | Where-Object -FilterScript {$_.Name -like “MSSQLSERVER“} | Select-Object -Property machineName,name,Displayname,status
}
The WAIT step is really creepy but had no idea how to do it other way Could you suggest a way i could rewrite it in order to stop the service, wait till it is stopped and then return state?
Thanks
If you are using the Stop
method from the ServiceController
class there is another method called WaitForStatus
. The 2 parameter overload is safer because you can specific a timeout so that your script doesn't hang if the service never stops.
$vmlist = Get-Content C:\xx.txt
foreach($VM in $VMlist) {
Get-Service -ComputerName $VM |
? { $_.Name -match “MSSQLSERVER” } | % {
$_.Stop()
$_.WaitForStatus('Stopped','00:00:05')
}
Get-Service -ComputerName $VM |
? { $_.Name -like “MSSQLSERVER“ } |
Select machineName, name, Displayname, status
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.