首页 > 解决方案 > Azure Runbook 查询运行作业

问题描述

Azure Powershell Runbook,计划每 1 小时运行一次。该脚本运行“Invoke-AzVMRunCommand”以在本地调用远程 VM 上的 Powershell 脚本。

问题 - 有时它运行时间超过 1 小时并与计划中的下一个重叠,第二次运行失败并出现与“Invoke-AzVMRunCommand”相关的错误: “运行命令扩展执行正在进行中。请等待完成后再调用运行命令。”

问题 - 如何查询 Runbook 作业当前是否正在运行。我们不能改变时间表。

谢谢你!

标签: powershellazure-runbook

解决方案


这个做检查:

$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

推荐阅读