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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$jobId = $null if ($PSVersionTable.PSVersion.Major -eq 5) { # Need the Guid specifically, unlike PowerShell 7.x $jobId = $PSPrivateMetadata.JobId.Guid } if ($PSVersionTable.PSVersion.Major -eq 7) { # We are in PowerShell 7.x if ($PSVersionTable.PSVersion.Minor -gt 1) { # We are in PowerShell 7.2 and above if ($env:AZUREPS_HOST_ENVIRONMENT -eq "AzureAutomation/") { # We are in a Hybrid Runbook Worker $jobId = $env:PSPrivateMetadata } else { # We are in Azure sandbox $jobId = $PSPrivateMetadata.JobId } } else { # This will be empty for PowerShell 7.1 running in Azure sandbox $jobId = $env:PSPrivateMetadata } } if ($jobId) { # Do something } |