Azure 自动化 - 身份验证循环

时间:2021-04-22 09:42:31

标签: azure

我在 Azure 自动化 Powershell 工作流方面遇到了重大问题。 我有一个工作流负责删除超过 48 小时的快照。 出于某种原因,当工作流运行时,当它在一次运行中多次向 Azure 进行身份验证时,我最终会出现一个循环。

所有模块都是最新的。

代码如下:

workflow Auto-Snapshot-Cleanup
   {
        #Authentication

        Write-Output ""
        Write-Output "------------------------ Authentication ------------------------"
        Write-Output "Logging into Azure ..."
        
        Clear-AzContext -Force

        try{
            # Ensures you do not inherit an AzContext in your runbook
            $null = Disable-AzContextAutosave -Scope Process
            $Conn = Get-AutomationConnection -Name AzureRunAsConnection
            
            $null = Connect-AzAccount `
                    -ServicePrincipal `
                    -Tenant $Conn.TenantID `
                    -ApplicationId $Conn.ApplicationID `
                    -CertificateThumbprint $Conn.CertificateThumbprint
    
            Write-Output "Successfully logged into Azure." 
        }
        catch{
            if (!$conn){
                $ErrorMessage = "Service principal not found."
                throw $ErrorMessage
            }
            else{
                Write-Error -Message $_.Exception
                throw $_.Exception
            }
        }
        ## End of authentication
    
        ## Getting all Resource Groups in the Test spoke
        Write-Output ""
        Write-Output ""
        Write-Output "---------------------------- Status ----------------------------"
        Write-Output "Getting all Resource Groups in the Test spoke ..."


         try{
            $groups = Get-AzResourceGroup

                foreach -parallel ($group in $groups){ 

                    #Delete snapshots older than 48 hours
                    
                    $snapshotList = Get-AzSnapshot -ResourceGroupName $using:group.ResourceGroupName | Where-Object {$_.Name -like 'autosnapshot*'}
                    $snapage = 48
                        foreach -parallel ($snap in $snapshotList){
                            InlineScript{
                                try{

                                    $start = $using:snap.TimeCreated
                                    $deletedsnap = $using:snap.name

                                    if (!((New-TimeSpan -Start ($start).AddHours(2) -End (get-date).ToString() ).TotalHours -lt $using:SnapAge)){    
                                        #Write-Output "Removing $deletedsnap"    
                                        Remove-AzSnapshot -SnapshotName $using:snap.Name -ResourceGroupName $using:snap.ResourceGroupName -Force
                                    }
                                }
                                catch{
                                    Write-Error -Message $_.Exception
                                    throw $_.Exception
                                }
                            }
                        }
            }
        }
        catch{
            Write-Error -Message $_.Exception
            throw $_.Exception}
 }

结果是这样的:

Azure Automation Authentication Loop

任何帮助将不胜感激。 亲切的问候, 沃伊切赫

0 个答案:

没有答案