2016-07-20 2 views
4

누구든지 나를 도와 줄 수 있는지 궁금합니다. 나는 서비스를 중지하려면이 문을 가지고 있지만 내가 시작 슬립 내가 후 루프를 중지 카운터를 추가 할 5PowerShell - while 루프에 카운터 추가

while($module | Get-Service | ? {$_.Status -eq "Running" -or $_.Status -eq "Stopping"}) 
{ 
    set-service $module -ComputerName $targetserver -StartupType Disabled -Status Stopped -PassThru 
     ; 
    Start-Sleep -Seconds 5 #keep repeating stop services every 5 seconds until all services have stopped 
} 

을 -s 추가하므로 시간에 서비스 때로는 종속성으로 인해 멈추지 않는다 60 초, 서비스가 계속 실행중인 경우 write-host r n "Service not stopped."

도움을 주시면 감사하겠습니다. 모두에게 감사드립니다.

+1

시간 .com/questions/3513650/timing-a-commands-in-powershell) – gravity

답변

4

60/5 = 12 루프.

while (test -and ($counter++ -lt 12)) { 
    #stop-service 
    #start-sleep 
} 

하지만 긴 테스트와 함께, 그것은 더 읽을 수 있습니다와 같은 : // 유래 : 당신의 [정지 시계 (또는 기타 유사한 내장 기능)] (HTTP를 꺼내

do 
{ 
    set-service $module -ComputerName $targetserver -StartupType Disabled -Status Stopped -PassThru 
    Start-Sleep -Seconds 5 

    $status = ($module | Get-Service).Status 
    $loopCounter++ 

} while ($status -in ('Running', 'Stopping') -and ($loopCounter -lt 12)) 
+0

이것은 내가 필요한 것입니다. 정말 고맙습니다! – Mike