我已准备好以下脚本来更改sql server服务帐户。但是当我在脚本下面运行时,服务不会停止和启动。任何迪亚?有什么方法可以做到这一点。睡觉有什么选择吗?我们不知道停止和启动需要多少服务。有没有办法让powershell等到服务完全 停止/启动。
$Services = Get-WmiObject Win32_Service -ComputerName "." | Where { $_.name -eq 'MSSQLSERVER' }
ForEach($Service in $Services)
{
$StopStatus = $Service.StopService()
Sleep 15
If ($StopStatus.ReturnValue -eq "0")
{write-host "$Service -> Service Stopped Successfully"}
$ChangeStatus = $Service.change($null,$null,$null,$null,$null,$null,$ServiceAccount,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq "0")
{write-host "$Service -> Sucessfully Changed Service Account"}
$StartStatus = $Service.StartService()
Sleep 25
If ($ChangeStatus.ReturnValue -eq "0")
{write-host "$Service -> Service Started Successfully"}
}
答案 0 :(得分:0)
您可以查看服务是否在循环中停止(或启动),然后继续:
$sleepCounter = 1
While((Get-Service $serviceName).status -ne "Stopped" ){
Write-Host "Waiting for service to stop. Attempt $sleepCounter of 20"
sleep 1
if($sleepCounter -eq 20) { break }
$sleepCounter++
}
此外,您可以执行以下操作来获取服务,而不是使用where-object
$svc=gwmi win32_service -filter "name='$serviceName'"