0
votes

Azure Powershell runbook, scheduled to run every 1h. The script runs "Invoke-AzVMRunCommand" to call a Powershell script on remote VM locally.

The problem - sometimes it runs longer than 1h and overlaps with next in the schedule, and the second run fails with an error related to "Invoke-AzVMRunCommand" : "Run command extension execution is in progress. Please wait for completion before invoking a run command."

The question - how to query if the runbook job is currently running. We can not change schedule.

thank you!

2

2 Answers

0
votes

You may use Az PowerShell cmdlet Get-AzAutomationJob to check the status of the job. Also, based on that status you may decide to remove or set existing schedule using schedule related cmdlets from here.

Hope this helps! Cheers!

0
votes

This one does the check:

$jobs = Get-AzAutomationJob -ResourceGroupName $rgName -AutomationAccountName $aaName -RunbookName $runbook

$runningCount = ($jobs | Where-Object { $_.Status -eq "Running" }).count

if (($jobs.status -contains "Running" -And $runningCount -gt 1 ) -Or ($jobs.Status -eq "New"))
{   
    Write-Output "`n This runbook [$runbook] execution is stopped - there is another job currently running. Execution will start as per schedule next hour."
    Exit 1
} 
else
{
    Write-Output "`n Let's proceed with runbook [$runbook] execution - there are no interfering jobs currently running." | Out-File -Filepath StarStop.txt -Append
} #end of check runbook status