Get Azure Webjobs failed runs using PowerShell

Todays article will show you how to get Azure webjobs failed runs using PowerShell.

Recently I was asked to prepare monitoring for Azure webjobs. I thought it will be quite simple and Application Insights can be used for this purpose. Unfortunetly to use Application Insights for this purpose additional development (telemetry implementation inside code) is needed.

How script work?

To get Azure webjobs failed runs script is connectig via API to WebApp and gathering all triggered web jobs.
In next step it checking history of each webjob and if some failed run is found it is added to array where all failed webjobs runs are stored.

Script which I prepared don’t need any special adjustment. As input parameters it requires 4 variables:
WebAppName – name of the WebApp on which webjobs are running
Username – name of the username used during deployment
Password – password of the above user
DaysBack – number of days back which should be searched for failed runs

Script:
#Provide input data
$WebAppName = 'Name_of_the_webapp'
$Username = '$username'
$Password = 'Password_used_during_webapp_deployment'
$DaysBack = 11

#Encode authorization information in base64
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username, $Password)))
$userAgent = "powershell/1.0"

$apiUrl = "https://$WebAppName.scm.azurewebsites.net/api/triggeredwebjobs"
$TriggeredWebJobs = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET

$TempStartDate = (Get-date).AddDays(-$DaysBack)
$StartDate = Get-Date $TempStartDate -Format yyyy-MM-dd
$ResultArray = @()
foreach ($job in $TriggeredWebJobs) {

    $jobName = $job.Name
    $jobHistoryUrl = "https://$WebAppName.scm.azurewebsites.net/api/triggeredwebjobs/$jobName/history"
    $JobLogs = Invoke-RestMethod -Uri $jobHistoryUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET
    $JobRuns = $JobLogs.Runs

    foreach ($jobRun in $JobRuns) {

        $StartTime = $jobRun.start_time.Split("T")[0]
        
        if (($jobRun.Status -eq "Failed") -and ($StartTime -ge $StartDate)) {
            
            $outputUrl = $jobRun.output_url
            $Output = Invoke-RestMethod -Uri $outputUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET
            
            $WebJob = [PSCustomObject]@{WebJobName = ''; WebJobOutput = ''; WebJobDate = ''}
            $WebJob.WebJobName = $jobName
            $WebJob.WebJobOutput = $Output
            $WebJob.WebJobDate = $jobRun.start_time
            
            $ResultArray += $WebJob
        }
    }
}

$ResultArray
Result:

Script of course can be easly adjusted for permanent monitoring and added in scheduler or implemented in Azure Automation.

I hope it will be usefull for some of you 😉
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.