title: Azure Runbook check status
body: <p>There is an Azure Automation runbook, which runs Powershell script below. It checks if runbook has running jobs already, and if no, execute some script.</p>
  <p>The problem:</p>
  <p>If I run it on &quot;Test Pane&quot; it works fine, but once I run via schedule or start as a job, it always exists with an output job is running.
  There are no jobs running and running code from my laptop it shows not running as well.
  Why it goes well on a Runbook test pane, but fails on normal run?</p>
  <pre><code>param (
  [string]$runbook = &quot;test-rb&quot;,
  [string]$rgName = &quot;test-rg&quot;,
  [string]$aaName = &quot;test-aa&quot;
  )
  
  $jobs = Get-AzAutomationJob -ResourceGroupName $rgName -AutomationAccountName $aaName -RunbookName $runbook
  #$Jobs.status
  
  # Check to see if it is already running
  
  if (($jobs.status -contains &quot;Running&quot;) -Or ($jobs.Status -eq &quot;New&quot;))
  {   
      Write-Output &quot;Runbook execution is stopped [$runbook] - there is another job currently running.&quot;
      exit 1
  } 
  else
  {
      Write-Output &quot;Proceed with runbook execution [$runbook]  - there are no interfering jobs running.&quot;
  }
  
  try {
      ....my script
  }
  catch {
      ....something something
  }
  </code></pre>