@@ -3,7 +3,7 @@ title: Manage runbooks in Azure Automation
3
3
description : This article tells how to manage runbooks in Azure Automation.
4
4
services : automation
5
5
ms.subservice : process-automation
6
- ms.date : 01/16/2022
6
+ ms.date : 06/29/2023
7
7
ms.topic : conceptual
8
8
ms.custom : devx-track-azurepowershell
9
9
---
@@ -171,34 +171,43 @@ Some runbooks behave strangely if they run across multiple jobs at the same time
171
171
# Ensures you do not inherit an AzContext in your runbook
172
172
Disable-AzContextAutosave -Scope Process
173
173
174
- # Connect to Azure with system-assigned managed identity
174
+ # Connect to Azure with system-assigned managed identity
175
175
$AzureContext = (Connect-AzAccount -Identity).context
176
176
177
- # set and store context
178
- $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription `
179
- -DefaultProfile $AzureContext
177
+ # set and store context
178
+ $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
180
179
181
- # Check for already running or new runbooks
182
- $runbookName = "runbookName"
183
- $resourceGroupName = "resourceGroupName"
180
+ # Check for already running or new runbooks
181
+ $runbookName = "runbookName"
182
+ $resourceGroupName = "resourceGroupName"
184
183
$automationAccountName = "automationAccountName"
185
184
186
- $jobs = Get-AzAutomationJob -ResourceGroupName $resourceGroupName `
187
- -AutomationAccountName $automationAccountName `
188
- -RunbookName $runbookName `
189
- -DefaultProfile $AzureContext
190
-
191
- # Check to see if it is already running
192
- $runningCount = ($jobs.Where( { $_.Status -eq 'Running' })).count
193
-
194
- if (($jobs.Status -contains 'Running' -and $runningCount -gt 1 ) -or ($jobs.Status -eq 'New')) {
195
- # Exit code
196
- Write-Output "Runbook $runbookName is already running"
197
- exit 1
198
- } else {
199
- # Insert Your code here
200
- Write-Output "Runbook $runbookName is not running"
185
+ $jobs = Get-AzAutomationJob -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName -RunbookName $runbookName -DefaultProfile $AzureContext
186
+
187
+ # Ranking all the active jobs
188
+ $activeJobs = $jobs | where {$_.status -eq 'Running' -or $_.status -eq 'Queued' -or $_.status -eq 'New' -or $_.status -eq 'Activating' -or $_.status -eq 'Resuming'} | Sort-Object -Property CreationTime
189
+ $jobRanking = @()
190
+ $rank = 0
191
+ ForEach($activeJob in $activeJobs)
192
+ {
193
+ $rank = $rank + 1
194
+ $activeJob | Add-Member -MemberType NoteProperty -Name jobRanking -Value $rank -Force
195
+ $jobRanking += $activeJob
201
196
}
197
+
198
+ $AutomationJobId = $PSPrivateMetadata.JobId.Guid
199
+ $currentJob = $activeJobs | where {$_.JobId -eq $AutomationJobId}
200
+ $currentJobRank = $currentJob.jobRanking
201
+
202
+ # Only allow the Job with Rank = 1 to start processing.
203
+ If($currentJobRank -ne "1")
204
+ {
205
+ Write-Output "$(Get-Date -Format yyyy-MM-dd-hh-mm-ss.ffff) Concurrency check failed as Current Job Ranking is not 1 but $($currentJobRank) therefore exiting..."
206
+ Exit
207
+ } Else
208
+ {
209
+ Write-Output "$(Get-Date -Format yyyy-MM-dd-hh-mm-ss.ffff) Concurrency check passed. Start processing.."
210
+ }
202
211
```
203
212
204
213
If you want the runbook to execute with the system-assigned managed identity, leave the code as-is. If you prefer to use a user-assigned managed identity, then:
0 commit comments