繁体   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