Как узнать, когда команда не отвечает в powershell - PullRequest
0 голосов
/ 02 апреля 2012

У меня есть скрипт powershell, который запускает утилиту командной строки DacIESvcCli.exe

DacIESvcCli.exe отправляет мне ответ, и когда я его получаю, я принимаю статус, который может быть «Работает» или «Завершено»

Моя проблема в том, что иногда звонок зависает, и я никогда не получаю ответ. Следующий скрипт может работать 3 дня без перерыва. Как я могу предотвратить это?

$myCounter = 0
while($myCounter -lt 5){
Write "start of the while counter : "  $myCounter
$exportResponse = C:\DAC\DacIESvcCli.exe -s "myserver.database.windows.net" -u "mylogin@myserver" -p "mypassword" -requestid 'e1e34eee-1aaa-4cc9-8c48-3a2239fe1bff' -status
$exportStatus = $exportResponse[10].split(" ")[1].toString() 
Write $exportStatus
$myCounter++
}

Вот вывод

start of the while counter :
0
Completed
start of the while counter :
1
Completed
start of the while counter :
2
Completed
start of the while counter :
3
_

... и это никогда не заканчивается.

1 Ответ

0 голосов
/ 02 июля 2012

Вот часть моего сценария для Трэвис Хесеман

  param($username, $password, $serverName, $requestId)
  $consecutiveFailedAttempt = 0 

while( $exportStatus -ne "Completed" -and $exportStatus -ne "Failed" -and $exportStatus -ne "TooLong"){  
    if($exportStatus -ne "FirstRun"){ 
        Start-Sleep -m 60000 # Wait 1 min  
    }

    $currentNow = Get-Date 

    $job = Start-Job {  param($s, $u, $p, $r) C:\DAC\DacIESvcCli.exe -s $s  -User $u  -p $p  -requestid $r -status } -ArgumentList @($serverName, $username, $password, $requestId)


    Wait-Job $job -Timeout 60 # if the command takes longer than 60 sec we timeout and retry
    Stop-Job $job 
    $exportResponse = Receive-Job $job
    Remove-Job $job 

    if($exportResponse[10]){
        $exportStatus = $exportResponse[10].split(" ")[1].toString()  
        $consecutiveFailedAttempt = 0;
    }else{
        $currentNow = Get-Date
        $whileMessage = "Time out we retry " + $currentNow   
        $whileMessage | Out-File $File -append
        $exportStatus = "Unknown"
        $consecutiveFailedAttempt++;
    }

    if($consecutiveFailedAttempt -gt 10){
        $exportStatus = "TooLong"
    }

}
# do wantever you want  with the export status
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...