Azure自动化帐户PowerShell Runbook自行重启

iklwldmw  于 2023-11-21  发布在  Shell
关注(0)|答案(2)|浏览(171)

我在Azure中有一个自动化帐户,有几个PowerShell Runbook。Runbook也在Azure中的Windows混合工作虚拟机上运行。除了一个Runbook之外,我的所有Runbook都运行得很好。问题Runbook运行得很好,但随后会重新启动自己,并且它会一遍又一遍地这样做。
Runbook中的相关代码是:

Initialize-Variables

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

$session_token = Get-Token

.\log-message.ps1 -RunbookVariables $script:CommonVars -Message "Polling ${script:secret_source} for certificates"

$devices = Get-Devices -session_token $session_token

$certificates = @();
$certificate_types = @("local", "remote", "ca", "pki", "ocsp-server");

foreach ($device in $devices) {
    $device_id = $device.vdom[0].devid;
    $device_certs = @();
    foreach ($cert_type in $certificate_types) {
        $type_certs = Get-DeviceCerts -session_token $session_token -device_id $device_id -cert_type $cert_type
        if ($null -eq $type_certs) { Continue }
        try {
            $device_certs += $type_certs.Where({ $null -ne $_._certinfo })
        }
        catch {
            $device_certs += @($type_certs).Where({ $null -ne $_._certinfo })
        }
    }

    .\log-message.ps1 -RunbookVariables $script:CommonVars -Message "Checking '${device_id}' for certificates"
    foreach ($cert in $device_certs) {
        $parsed_cert = Set-Certificate -cert $cert -device_id $device_id;
        # Confirm the cert is not a duplicate
        if (!(Find-Duplicate -certificates $certificates -cert $parsed_cert)) {
            $certificates += @($parsed_cert)
        }
    }
}

Remove-Token -session_token $session_token

.\log-message.ps1 -RunbookVariables $script:CommonVars -Message "${script:secret_source} Certificates polled"
.\log-message.ps1 -RunbookVariables $script:CommonVars -Message "Checking delta table for diffs"

$script:CommonVars["secrets"] = $certificates

.\manage-secret-database.ps1 -RunbookVariables $script:CommonVars
.\log-message.ps1 -RunbookVariables $script:CommonVars -Message "FINISHED POLLING CERTIFICATES FOR FORTIGATE"

# Nullify sensitive vars, letting the garbage collector know they're ready to be collected
$script:db_client_secret = $null
$script:db_password = $null
$script:fortimanager_password = $null

# Nullify secret array, letting the garbage collector know it's ready to be collected
# The secret array doesn't contain any sensitive info per-se, but since it's metadata on secrets/certs, it's best to clear it anyway
$script:secrets = $null
$script:certificates = $null

.\log-message.ps1 -RunbookVariables $script:CommonVars -Message "Starting Garbage Collection"
# Force Garbage collection
[GC]::Collect()

# FIXME: unless we explicitly write output at the end of this specific script, it will call itself infinitely
Start-Sleep 10
Write-Output "Attempting to Exit"
Exit 0

字符串
该脚本调用了其他一些runbook log-messageinitialize-common-varsmanage-secret-database。但其他runbook调用这些相同的runbook而不重新启动。我认为问题在于脚本需要输出 * 一些东西 *,所以我添加了逻辑来编写一些输出,然后显式退出,但输出被写入,然后runbook重新启动。
我问了ChatGPT,我在谷歌上搜索了高和低,但找不到解决方案。有什么想法吗?

wmomyfyw

wmomyfyw1#

删除脚本结尾处的Start-Sleep 10Exit 0,因为这可能会导致重新启动。在Azure自动化中,当脚本到达结尾时,脚本执行被视为完成。添加sleep和exit可能会导致Runbook重新启动。
相反,您可以在脚本中使用Start-SleepWait-Event来确保延迟或等待特定条件。

pod7payv

pod7payv2#

我能够解决这个问题。问题是,即使使用Hybrid Worker,如果Runbook生成子Runbook,则该子Runbook只有10分钟的执行时间。如果子Runbook未能在10分钟内完成,则父Runbook将重新启动(而不会杀死子Runbook的进程)

相关问题