skip to Main Content

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

How do we get it if the runbook is Python?

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search