为什么带有Start-Job,Wait-Job的简单Powershell中断器不会返回结果?

时间:2016-01-25 22:19:44

标签: powershell wait

我们有一个jenkins git工作,每隔一段时间就会挂在远程检查上。所以我想暂停中断检查。

据我所知,如果计时器必须停止等待后台作业,则wait-job将返回null。因此,当代码块长时间运行时,它应返回null。这对我来说在命令行中很有用。

但是,当作业很短时,当我在ISE中运行该函数时,下面的代码仍然为空。当我调试它,它工作正常。救命? 谢谢! 安

function Test-TimeoutJob {
<#
.EXAMPLE
BUG:
> Test-TimeoutJob -theCodeBlock {write-output 'hi'}
Test-TimeoutJob : Type of theCodeBlock= scriptblock ; Text= write-output 'hi' .
Test-TimeoutJob : Return code from starting the job =  , True .

Id     Name            PSJobTypeName   State         HasMoreData     Location           
--     ----            -------------   -----         -----------     --------           
16     Job16           BackgroundJob   Running       True            localhost          
Test-TimeoutJob : Got null output of wait-job on id# 16 . 
End : Now= 01/25/2016 17:12:02
#>
    param (
        $theCodeBlock       ,  # infinite; $i=0; do {$i++; echo $i; } while ($true)
        $theTimeoutSeconds =  1 # Beware; default=-1sec means wait infinitely #3
    )
    $thisFcn = 'Test-TimeoutJob'

    # If null input, then set it.
    if ( ! $theCodeBlock ) {
        $theCodeBlock = {
            #Show-TimeNow -theMessage "CodeBlock"
            #Start-Sleep -Seconds 10
            # your commands here, e.g.
            Get-ChildItem *.cs | select name
        }    
    }
    $theCodeType = $theCodeBlock.GetType()
    $theCodeStr  = $theCodeBlock.ToString()
    Write-Host "$thisFcn : Type of theCodeBlock= $theCodeType ; Text= $theCodeStr ."
    $theJob = Start-Job -ScriptBlock $theCodeBlock
    write-host "$thisFcn : Return code from starting the job = $LASTEXITCODE , $? ."
    $jobid  = $theJob.Id
    Get-Job   $jobid
    $answaitobj = Wait-Job $theJob.Id -Timeout $theTimeoutSeconds
    if ( $answaitobj -eq $null ) { 
        Write-Host "$thisFcn : Got null output of wait-job on id# $jobid . "
     }
    elseif ( $answaitobj ) { 
        $jobStatus = $theJob.State
        $anstype = $answaitobj.GetType()
        Write-Host "$thisFcn : the answer, supposed to be job, has type= $anstype ; status= $jobStatus ."
        Stop-Job $theJob 
        $ansId = $theJob.Id
        Write-Host "Job $ansId has been ended, with status= $jobStatus ; Thus it has finished or been stopped due to timeout."

        # For our purposes of abending a script, we do not need to 
        # either get its data, which is null, or cleanup, which automatically occurs
        # once jenkins finishes the call to posh.

        # Get first element of output iterable
        #$ans = Receive-Job -Job $theJob  -Keep
        #Remove-Job -force $theJob 
    }

    Show-TimeNow -theMessage 'End'
}


function Show-TimeNow {
    param ( 
        $theMessage = 'Hello from Show-TimeNow'
    )
    $now = Get-Date
    Write-Output "$theMessage : Now= $now"
}

2 个答案:

答案 0 :(得分:0)

如果超时到期,documentation永远不会承诺Wait-Job会返回任何内容,那么为什么你会期望呢?

  

<强> -Timeout&LT;&的Int32 GT;

     

确定每个后台作业的最长等待时间(以秒为单位)。无论运行多长时间,默认值-1都会等到作业完成。提交Wait-Job命令而不是Start-Job命令时,计时开始。

     

如果超过此时间,即使作业仍在运行,等待也会结束并返回命令提示符。没有显示错误消息。

Wait-Job返回后检查作业的状态,以确定作业是否完成:

if ($job.State -eq 'Completed') {
  Write-Host 'Finished.'
} else {
  Write-Host 'Timeout expired. Stopping job.'
  Stop-Job -Id $job.Id
}

答案 1 :(得分:0)

答案结果表明,在我的系统上,即使对于最简单的程序,一秒钟也不会超时。将超时设置为更长的秒数会返回预期结果。