Skip to content

Instantly share code, notes, and snippets.

@rakheshster
Created April 2, 2022 17:47
Show Gist options
  • Save rakheshster/64cbf5bff56fe6c6c9aff40f43736d73 to your computer and use it in GitHub Desktop.
Save rakheshster/64cbf5bff56fe6c6c9aff40f43736d73 to your computer and use it in GitHub Desktop.
# Get the Job ID from PSPrivateMetadata. That's the only thing it contains!
$automationJobId = $PSPrivateMetadata.JobId.Guid
# Maximum number of instances of this Runbook I want running at any given time
$numberOfInstances = 1
# Connect to Azure. With a Managed Identity in this case as that's what I use.
# Must connect to Azure before running Get-AzAutomationJob or Get-AzResource
try {
# From https://docs.microsoft.com/en-us/azure/automation/enable-managed-identity-for-automation#authenticate-access-with-system-assigned-managed-identity
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process | Out-Null
# Connect to Azure with system-assigned managed identity
Connect-AzAccount -Identity | Out-Null
} catch {
Write-Error "Runbook could not connect to Azure: $($_.Exception.Message)"
exit
}
# Get all Runbooks in the current subscription
$allAutomationAccounts = Get-AzResource -ResourceType Microsoft.Automation/automationAccounts
$automationAccountName = $null
$resourceGroupName = $null
$runbookName = $null
foreach ($automationAccount in $allAutomationAccounts) {
$runbookJob = Get-AzAutomationJob -AutomationAccountName $automationAccount.Name `
-ResourceGroupName $automationAccount.ResourceGroupName `
-Id $automationJobId `
-ErrorAction SilentlyContinue
if (!([string]::IsNullOrEmpty($runbookJob))) {
$automationAccountName = $runbookJob.AutomationAccountName
$resourceGroupName = $runbookJob.ResourceGroupName
$runbookName = $runbookJob.RunbookName
}
}
# At this point I'll have the Automation Account Name, Runbook Name, Job ID and Resource Group Name,
# Find all other active jobs of this Runbook.
$allActiveJobs = Get-AzAutomationJob -AutomationAccountName $automationAccountName `
-ResourceGroupName $resourceGroupName `
-RunbookName $runbookName |
Where-Object { ($_.Status -eq "Running") -or ($_.Status -eq "Starting") -or ($_.Status -eq "Queued")}
$oldestJob = $AllActiveJobs | Sort-Object -Property CreationTime | Select-Object -First 1
# If this job is not the oldest created job we will wait until the existing jobs complete or the number of jobs is less than numberOfInstances
while (($AutomationJobID -ne $oldestJob.JobId) -and ($allActiveJobs.Count -ge $numberOfInstances)) {
Write-Warning "Waiting as there are currently running $($allActiveJobs.Count) active jobs for this runbook already. Sleeping 30 seconds..."
Write-Warning "Oldest Job is $($oldestJob.JobId)"
Start-Sleep -Seconds 30
$allActiveJobs = Get-AzAutomationJob -AutomationAccountName $automationAccountName `
-ResourceGroupName $resourceGroupName `
-RunbookName $runbookName |
Where-Object { ($_.Status -eq "Running") -or ($_.Status -eq "Starting") -or ($_.Status -eq "Queued")}
$oldestJob = $allActiveJobs | Sort-Object -Property CreationTime | Select-Object -First 1
}
Write-Output "Job can continue..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment