Azure Runbook 检查状态
Azure Runbook check status
有一本 Azure 自动化 运行 书籍,下面是 运行 的 Powershell 脚本。它检查 运行book 是否已经有 运行ning 作业,如果没有,则执行一些脚本。
问题:
如果我 运行 它在“测试窗格”上它工作正常,但是一旦我 运行 通过计划或作为工作开始,它总是存在,输出工作是 运行宁。
我的笔记本电脑上没有作业 运行ning 和 运行ning 代码,它也显示没有 运行ning。
为什么它在 Runbook 测试面板上运行良好,但在正常 运行?
上失败
param (
[string]$runbook = "test-rb",
[string]$rgName = "test-rg",
[string]$aaName = "test-aa"
)
$jobs = Get-AzAutomationJob -ResourceGroupName $rgName -AutomationAccountName $aaName -RunbookName $runbook
#$Jobs.status
# Check to see if it is already running
if (($jobs.status -contains "Running") -Or ($jobs.Status -eq "New"))
{
Write-Output "Runbook execution is stopped [$runbook] - there is another job currently running."
exit 1
}
else
{
Write-Output "Proceed with runbook execution [$runbook] - there are no interfering jobs running."
}
try {
....my script
}
catch {
....something something
}
因为当你 运行 作业时,它会通过命令 Get-AzAutomationJob
将自己作为 Running
作业,在你的脚本中添加 Write-Output $jobs
,请参阅下面的屏幕截图,请注意 Id
和 JobId
是相同的。
要解决此问题,您可以使用 $jobs = $jobs[1..($jobs.Length-1)]
,它会自行排除,然后脚本将正常运行。
我的样本:
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$null = Add-AzAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$jobs = Get-AzAutomationJob -ResourceGroupName <resourcegroup-name> -AutomationAccountName joyauto1 -RunbookName test1
$jobs = $jobs[1..($jobs.Length-1)]
if (($jobs.status -contains "Running") -Or ($jobs.Status -eq "New"))
{
Write-Output "Runbook execution is stopped [] - there is another job currently running."
exit 1
}
else
{
Write-Output "Proceed with runbook execution [] - there are no interfering jobs running."
}
有一本 Azure 自动化 运行 书籍,下面是 运行 的 Powershell 脚本。它检查 运行book 是否已经有 运行ning 作业,如果没有,则执行一些脚本。
问题:
如果我 运行 它在“测试窗格”上它工作正常,但是一旦我 运行 通过计划或作为工作开始,它总是存在,输出工作是 运行宁。 我的笔记本电脑上没有作业 运行ning 和 运行ning 代码,它也显示没有 运行ning。 为什么它在 Runbook 测试面板上运行良好,但在正常 运行?
上失败param (
[string]$runbook = "test-rb",
[string]$rgName = "test-rg",
[string]$aaName = "test-aa"
)
$jobs = Get-AzAutomationJob -ResourceGroupName $rgName -AutomationAccountName $aaName -RunbookName $runbook
#$Jobs.status
# Check to see if it is already running
if (($jobs.status -contains "Running") -Or ($jobs.Status -eq "New"))
{
Write-Output "Runbook execution is stopped [$runbook] - there is another job currently running."
exit 1
}
else
{
Write-Output "Proceed with runbook execution [$runbook] - there are no interfering jobs running."
}
try {
....my script
}
catch {
....something something
}
因为当你 运行 作业时,它会通过命令 Get-AzAutomationJob
将自己作为 Running
作业,在你的脚本中添加 Write-Output $jobs
,请参阅下面的屏幕截图,请注意 Id
和 JobId
是相同的。
要解决此问题,您可以使用 $jobs = $jobs[1..($jobs.Length-1)]
,它会自行排除,然后脚本将正常运行。
我的样本:
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$null = Add-AzAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$jobs = Get-AzAutomationJob -ResourceGroupName <resourcegroup-name> -AutomationAccountName joyauto1 -RunbookName test1
$jobs = $jobs[1..($jobs.Length-1)]
if (($jobs.status -contains "Running") -Or ($jobs.Status -eq "New"))
{
Write-Output "Runbook execution is stopped [] - there is another job currently running."
exit 1
}
else
{
Write-Output "Proceed with runbook execution [] - there are no interfering jobs running."
}