title: Get job status in Azure Runbook
body: <p>I would like to have a runbook run on a schedule but exit if the last job hasn't finished running (i.e. if the schedule is every 2 hours and a job takes 2.5 hours then the next job shouldn't run).</p>
  
  <p>I've tried to use Get-AzureAutomationJob to get the last job status (<a href="https://azure.microsoft.com/en-gb/documentation/articles/automation-runbook-execution/#retrieving-job-status-using-windows-powershell" rel="nofollow noreferrer">https://azure.microsoft.com/en-gb/documentation/articles/automation-runbook-execution/#retrieving-job-status-using-windows-powershell</a>), but I can't get it to work. I presume all the preamble to get the subscription etc is required.</p>
  
  <p>"Get-AzureAutomationJob : The Automation account was not found."</p>
  
  <pre><code>$ConnectionAssetName = "AzureClassicRunAsConnection"
  
  # Get the connection
  $connection = Get-AutomationConnection -Name $connectionAssetName        
  
  # Authenticate to Azure with certificate
  Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
  $Conn = Get-AutomationConnection -Name $ConnectionAssetName
  if ($Conn -eq $null)
  {
      throw "Could not retrieve connection asset: $ConnectionAssetName. Assure            that this asset exists in the Automation account."
  }
  
  $CertificateAssetName = $Conn.CertificateAssetName
  Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose
  $AzureCert = Get-AutomationCertificate -Name $CertificateAssetName
  if ($AzureCert -eq $null)
  {
      throw "Could not retrieve certificate asset: $CertificateAssetName.       Assure that this asset exists in the Automation account."
  }
  
  Write-Verbose "Authenticating to Azure with certificate." -Verbose
  Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -      SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert 
  Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID
  
  $job = (Get-AzureAutomationJob –AutomationAccountName "THE NAME OF THE AUTOMATION ACCOUNT AS IT APPEARS IN THE PORTAL" –Name "JobStatusTest" | sort LastModifiedDate –desc)[0]
  </code></pre>