How to get Job Details when executing an Azure Automation Runbook

BRUNO Tonino (ENGIE IT) 21 Reputation points
2024-11-25T21:19:08.9766667+00:00

Hi,

I am running a PowerShell based Runbook in Azure Automation on a Hybrid Worker and I would like to retrieve the curent job details like the JobId, JobName for ex. I am trying to record the job details and health to a SQL table to help me track stale jobs and to restart them automatically.

Is there a way to retrieve that information easily using PowerShell from within the Job itself?

Sincerely,

Tonino Bruno

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,272 questions
0 comments No comments
{count} votes

Accepted answer
  1. hossein jalilian 8,840 Reputation points
    2024-11-25T22:15:42.7866667+00:00

    Hello BRUNO Tonino (ENGIE IT),

    Thanks for posting your question in the Microsoft Q&A forum.

    You can use PowerShell commands. first, ensure that your runbook is connected to your Azure account. This can be done using the Connect-AzAccount cmdlet if necessary.

    Use the Get-AzAutomationJob cmdlet to fetch details about the current job. You will need to specify the resource group name, automation account name, and optionally the runbook name.

    # Variables
    $resourceGroupName = "yourResourceGroupName"
    $automationAccountName = "yourAutomationAccountName"
    $runbookName = "yourRunbookName"
    
    # Get the job details
    $job = Get-AzAutomationJob -ResourceGroupName $resourceGroupName `
                               -AutomationAccountName $automationAccountName `
                               -RunbookName $runbookName
    
    # Output job details
    $jobId = $job.JobId
    $jobName = $job.Runbook.Name
    
    Write-Output "Job ID: $jobId"
    Write-Output "Job Name: $jobName"
    
    

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.