簡體   English   中英

Azure 自動化操作手冊、Connect-AzAccount、分配權限

[英]Azure Automation Runbooks, Connect-AzAccount, Assigning rights

我正在嘗試使用 Azure Automation 進行一些基本的組管理,但我很難讓腳本正確進行身份驗證。

我已將 Azure.Account 模塊添加到運行手冊中,連接似乎已建立(至少,它不會引發異常,並且返回的 object 不為空)。

使用“Get-AzAdGroup”時,我得到:

Get-AzADGroup : Insufficient privileges to complete the operation.

創建的應用程序帳戶是 AAD 中的“貢獻者”,據我所知,對該目錄具有完整的權限。

我已經嘗試了How to connect-azaccount in Azure DevOps release pipeline 中列出的解決方案,效果相同(權限不足)。 我還應用了“Group.Read.All”、“Group.ReadWrite.All”、“GroupMember.Read.All”、“GroupMember.ReadWrite.All”,基於我可以從https://learn.microsoft 讀取的內容。 com/en-us/graph/permissions-reference#group-permissions - 但我不是 100% 清楚 Az* cmdlet 是否使用 Microsoft Graph,或者它們是否完全分開。

代碼如下:

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

    "Logging in to Azure..."
    <#
    # Original, technically legacy. 
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
    #>

    $connectState = Connect-AzAccount `
       -ServicePrincipal `
       -TenantId $servicePrincipalConnection.TenantId `
       -ApplicationId $servicePrincipalConnection.ApplicationId `
       -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint


    <#
    # From https://stackoverflow.com/questions/56350960/how-to-connect-azaccount-in-azure-devops-release-pipeline, same result.
    $AzurePassword = ConvertTo-SecureString "*****" -AsPlainText -force
    $psCred = New-Object System.Management.Automation.PSCredential($servicePrincipalConnection.ApplicationId , $AzurePassword)
    $connectState = Connect-AzAccount -Credential $psCred -TenantId $servicePrincipalConnection.TenantId  -ServicePrincipal 
    #>

    if ($connectState) {
        "Connected."
    } else {
        "Doesn't seem to be connected."
    }
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

# Get groups
Get-AzADGroup

我的直覺告訴我,由於兩種 connect-azaccount 方法都產生相同的結果(已連接,但無法訪問),我的問題不一定在腳本中,而是缺少創建服務帳戶(這對 MFA 提出了挑戰),我不不知道如何解決這個問題。

從我提供的解決方案How to connect-azaccount in Azure DevOps release pipeline中,在屏幕截圖中,很明顯您需要添加 API 權限Azure Active Directory Graph ,而不是Microsoft Graph

請在Azure Active Directory Graph中添加Directory.Read.All為您的自動化運行帳戶的 AD 應用程序。

在此處輸入圖像描述

我遇到了一個非常相似的問題。 你APP擁有的API權限Azure有問題。

在我的例子中,我的azure 應用程序作為服務主體工作,不僅修改了Azure AD中的一些內容,還修改了一些Azure 資源,因此,這些是我必須授予的 api 權限: 在此處輸入圖像描述

請記住,您還需要向 Azure 租戶授予管理員同意才能進行這些權限更新。

如果您只是將Contributor角色分配給服務主體,則可以使用 sp 獲取 Azure 資源(如 VM、應用服務)。 因此,如果要使用 sp tp get Azure AD 資源,我們需要為 sp 分配 Azure AD 角色(例如 Directory Readers)。 更多細節請參考文檔文檔

詳細步驟如下

  1. 獲取 RunAsAccount sp object id

在此處輸入圖像描述

在此處輸入圖像描述

  1. 為應用程序配置權限
  connect-AzureAD

$sp=Get-AzureADServicePrincipal -ObjectId <the sp object id your copy>

$role=Get-AzureADDirectoryRole | Where-Object{$_.DisplayName -eq "Directory Readers"}
Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId  -RefObjectId $sp.ObjectId
  1. 測試一個。 創建一個新的運行手冊

     $servicePrincipalConnection=Get-AutomationConnection -Name 'AzureRunAsConnection' $connectState = Connect-AzAccount ` -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint if ($connectState) { "Connected." } else { "Doesn't seem to be connected." } Get-AzADGroup

    在此處輸入圖像描述

Go to Azure portal --> Azure AD --> roles and Administrator-->Directory Readers role --> assign this role to the runbook account name

暫無
暫無

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

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