Azure Automation PowerShell variables

I updated an older blog post of mine with new info thanks to a reader. That, plus the fact that I am updating my own runbooks made me capture some of the environment and other variables in Azure Automation for my own reference. Thought I’d put them here as a table.

Variable PowerShell 7.2 (Azure) PowerShell 7.2 (HRW) PowerShell 5.x (Azure) PowerShell 5.x (HRW) PowerShell 7.1 (Azure) PowerShell 7.1 (HRW)
$env:AZUREPS_HOST_ENVIRONMENT AzureAutomation AzureAutomation/ AzureAutomation AzureAutomation/ AzureAutomation Not present
$env:COMPUTERNAME SANDBOXHOST-*** The name of the HRW machine SANDBOXHOST-*** The name of the HRW machine SANDBOXHOST-*** The name of the HRW machine
$env:PSPrivateMetadata Not present Job Id Not present Not present Not present Job Id
$PSPrivateMetadata.JobId Job Id Not present Job Id Job Id Not present Not present
$PSVersionTable Present and contains info Present and contains info Present and contains info Present and contains info Present and contains info Present and contains info

So it looks like PowerShell 7.1 running on Azure sandbox has no correct way of finding the job Id of the running job. It’s not supported, so not much of an issue I guess – best to use PowerShell 7.2 instead of 7.1.

I think the following code can be used to find the job Id.