如何知道命令在powershell中没有响应的时间

时间:2012-04-02 09:53:22

标签: powershell

我有一个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 :(得分:0)

以下是我Travis Heseman

脚本的一部分
  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