Reputation: 12526
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
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
Reputation: 460
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
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.
Upvotes: 1