title: Why does a simple Powershell Interrupter with Start-Job, Wait-Job not return results?
body: <p>We have a jenkins git job that hangs on remote checking every so often. So I want to abort the checking with a timeout. </p>
  
  <p>I understand that wait-job will return null if the timer had to stop the background job being awaited. So when a code block is long-running, it should return a null. That works for me in the command line.</p>
  
  <p>However, when a job is short, the below code still getting null when I run the function in the ISE. When I debug it, it works fine. Help?
  thank you!
  Anne</p>
  
  <pre><code>function Test-TimeoutJob {
  &lt;#
  .EXAMPLE
  BUG:
  &gt; 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
  #&gt;
      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"
  }
  </code></pre>