2012-04-02 64 views
0

響應我有啓動命令行實用程序PowerShell腳本DacIESvcCli.exe如何知道當一個命令在PowerShell中

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 "[email protected]" -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 
_ 

...它永遠不會結束。

+3

檢查這些cmdlet我的腳本的一部分: 開始作業等待工作檢查出這篇文章,例如:http://stackoverflow.com/questions/4650913/how- to-timeout-powershell-function-calling – 2012-04-02 11:22:38

+0

謝謝我發現如何做到這一點與您的答案 – alexandrekow 2012-04-02 12:36:26

+0

@TimPost - 爲什麼你刪除user1027785的答案?從上面的評論(和評論投票)看,它顯然足以回答OP的問題。未來可能會很高興看到刪除別人答案的理由。我不想成爲一個混蛋。我想知道什麼時候刪除答案是適當的。 – 2012-04-02 21:36:50

回答

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 
相關問題