Skip to content

Commit aad281c

Browse files
Merge pull request #243458 from SnehaSudhirG/29June-ManageRunbook
updated the script
2 parents f9e06f4 + 742393a commit aad281c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

articles/automation/manage-runbooks.md

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Manage runbooks in Azure Automation
33
description: This article tells how to manage runbooks in Azure Automation.
44
services: automation
55
ms.subservice: process-automation
6-
ms.date: 01/16/2022
6+
ms.date: 06/29/2023
77
ms.topic: conceptual
88
ms.custom: devx-track-azurepowershell
99
---
@@ -171,34 +171,43 @@ Some runbooks behave strangely if they run across multiple jobs at the same time
171171
# Ensures you do not inherit an AzContext in your runbook
172172
Disable-AzContextAutosave -Scope Process
173173
174-
# Connect to Azure with system-assigned managed identity
174+
# Connect to Azure with system-assigned managed identity
175175
$AzureContext = (Connect-AzAccount -Identity).context
176176
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
180179
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"
184183
$automationAccountName = "automationAccountName"
185184
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
201196
}
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+
}
202211
```
203212

204213
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

Comments
 (0)