Throttling concurrent Azure Automation Runbooks

I don’t take any credit for this post. It’s mostly stuff I learnt from Google searches, and a script I put together for my use from other places. I should have credited them, but all this was put together over time and today I had to do it again and that’s when I went through my past snippets etc. and created something in a formal way.

Anyways, here’s the issue. I have a bunch of Azure Automation Runbooks that run on a schedule. Thing is, sometimes a Runbook takes longer than expected and so while it is running another one launches. This leads to both of them slowing down or conflicting with each others tasks and soon I have a whole bunch of Runbooks launching and making a mess of things. I need a way of ensuring only one Runbook is active at a time.

There’s a variable within Runbooks called PSPrivateMetadata. It doesn’t have anything except the JobId. You can extract this via $PSPrivateMetadata.JobId.Guid. Then you must search all running jobs to find the Automation Account name, Runbook name, and Resource Group name of this Runbook; and using that find what other jobs are running. And then decide what to do – wait for quit. Simple really. Wish the variable had the various names so I can save some trouble finding them… but oh well.

Here’s what I came up with:

Will update this in case of any bugs later.

I put a line like this before my main script in the Runbook:

And that’s it.

Update (17th July 2023): Looks like PowerShell 7.1 and 7.2 Runbooks dont have the PSPrivateMetadata variable. Here’s the output of Get-Variable on a PowerShell 7.2 HRW:

But PowerShell 5.1 has it.

So the above function won’t work on 7.x for now.