簡體   English   中英

Azure 自動化 Runbook 工作流丟失 AzContext

[英]Azure Automation Runbook Workflow looses AzContext

我編寫了以下 Runbook 工作流,但有時我會在嘗試啟動或停止 VM 時看到錯誤:

Start-AzVM:您的 Azure 憑證尚未設置或已過期,請運行 Connect-AzAccount 以設置您的 Azure 憑證。 在 StartStopVmByTag:46 char:46 + + CategoryInfo: CloseError: (:) [Start-AzVM], ArgumentException + FullyQualifiedErrorId: Microsoft.Azure.Commands.Compute.StartAzureVMCommand

我已嘗試傳入 $azContext 變量,但我仍然遇到此問題,我該如何進一步調查?

workflow StartStopVmByTag {
    $connectionName = "AzRunAsConnection2042";

    try {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName

        Write-Output "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
        }
    }

    [DateTime]$now = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'GMT Standard Time')
    $startTag = 'Start Schedule'

    Write-Output "*** $now - Runbook Started  ***"

    # Get Subscriptions
    $Subscriptions = Get-AzSubscription

    ForEach ($Subscription in $Subscriptions) {
        $azContext = Set-AzContext -SubscriptionId $Subscription.Id

        # Get all VM's with a Start or Stop Schedule
        Write-Output "$($Subscription.Name): Getting VM's..."
        [Array]$taggedVms = Get-AzResource -TagName $startTag -ResourceType 'Microsoft.Compute/virtualMachines'
        $taggedVms = $taggedVms | Sort-Object -Property Name -Unique

        # For each VM, check if start schedule is valid for now
        Foreach -Parallel ($taggedVm in $taggedVms) {
            Write-Output "$($Subscription.Name): Found Tagged VM: $($taggedVm.Name), $($startTag): $($taggedVm.Tags.$startTag -replace '\s', '')"
            $WORKFLOW:null = Start-AzVM -ResourceGroupName $taggedVm.ResourceGroupName -Name $taggedVm.Name -DefaultProfile $azContext -NoWait
        }
    }
}

一段時間以來,我一直在為這個問題苦苦掙扎,我嘗試了數十種不同的解決方法,但都沒有奏效。 我最終通過這些強制 .NET 應用程序使用 TLS 1.2 的注冊表設置解決了這個問題。 我覺得這個解決方案有效很奇怪,但可能是因為設置為任何父任務一部分的 TLS 1.2 沒有傳遞給作業。

它們可能並不都是必需的,但無論如何這似乎是最近的最佳實踐。

set-itemproperty "HKLM:\SOFTWARE\Microsoft\.NETFramework\v2.0.50727" -name SystemDefaultTlsVersions -value 1 -Type DWord
set-itemproperty "HKLM:\SOFTWARE\Microsoft\.NETFramework\v4.0.30319" -name SchUseStrongCrypto -value 1 -Type DWord
set-itemproperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v2.0.50727" -name SystemDefaultTlsVersions -value 1 -Type DWord
set-itemproperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319" -name SchUseStrongCrypto -value 1 -Type DWord

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM