powershell Azure Runbook查询运行作业

np8igboo  于 2023-01-13  发布在  Shell
关注(0)|答案(2)|浏览(125)

Azure Powershell Runbook,计划每1小时运行一次。该脚本运行"Invoke-AzVMRunCommand"以本地调用远程VM上的Powershell脚本。
问题-有时它运行的时间超过1h,并与计划中的下一个重叠,第二次运行失败,并出现与"Invoke-AzVMRunCommand"相关的错误:* "正在执行运行命令扩展。请等待完成后再调用运行命令。"*
问题-如何查询Runbook作业当前是否正在运行。我们无法更改计划。
谢谢大家!

3duebb1j

3duebb1j1#

这个人负责检查:

$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
wi3ka0sx

wi3ka0sx2#

你可以使用Az PowerShell cmdlet Get-AzAutomationJob检查作业的状态。此外,根据该状态,你可以决定使用here中与计划相关的cmdlet删除或设置现有计划。

相关问题