richard
richard

Reputation: 12526

How to get the Azure Automation Runbook Job Id in a Python Runbook?

Powershell Runbooks allow you to get the Job ID with $PSPrivateMetadata.JobId.Guid

How do we get it if the runbook is Python?

Upvotes: 1

Views: 592

Answers (3)

Lain
Lain

Reputation: 1

Linux:

import sys, string, os, subprocess

jobid=subprocess.check_output('pwsh -cwa "$PSPrivateMetadata.JobId.Guid"',shell=True) #calls a powershell subprocess and the output is stored on jobid

print(jobid) #This is should be the Job ID

Edit:

Windows: The command line to run powershell command in Windows CMD should be like this:

import sys, string, os, subprocess
jobif=subprocess.check_output('powershell -command "$PSPrivateMetadata.JobId.Guid"', shell=True) 
print(jobid)

Upvotes: 0

This is what you need.

To retrieve the Azure Automation Runbook Job Id in a Python Runbook, you can use the AutomationJobId environment variable that is automatically provided by Azure Automation. Here's how you can access the Runbook Job Id in a Python script within an Azure Automation Runbook:

import os

#get the Runbook Job Id from the AutomationJobId environment variable
runbook_job_id = os.environ.get('AutomationJobId')

if runbook_job_id:
    print(f'Runbook Job Id: {runbook_job_id}')
else:
    print('Runbook Job Id not found')

In the code snippet, I used the os.environ.get() method to retrieve the value of the AutomationJobId environment variable, which contains the unique identifier of the current job being executed in Azure Automation. You can then use this runbook_job_id variable to track or log the job id as needed within your Python script.

Upvotes: 2

Jason Ramboz
Jason Ramboz

Reputation: 54

According to the API documentation, you can create a Python Runbook using runbook = azure.mgmt.automation.models.Runbook(<options>), then access the runbook.id property.

Source: https://learn.microsoft.com/en-us/python/api/azure-mgmt-automation/azure.mgmt.automation.models.runbook?view=azure-python

Upvotes: 1

Related Questions