簡體   English   中英

如何使用 Powershell Function App 從 AAD 獲取組?

[英]How do I get groups from AAD using Powershell Function App?

我想使用 Powershell 和 Function 應用程序從 AAD 中的某些組中獲取所有用戶,但我不斷收到權限錯誤,我不知道如何分配它們。

$groupsAD = [System.Collections.ArrayList]@()
      $groupsAD.Add('Group1')
      $groupsAD.Add('Group2')

foreach ($groupAD in $groupsAD) {
    $group = Get-AzADGroup -DisplayName $groupAD
    # further code

}

錯誤:

[錯誤] 錯誤:權限不足,無法完成操作。異常:類型:System.ExceptionMessage:權限不足,無法完成操作。HResult:-2146233088CategoryInfo:InvalidOperation:(:) [Get-AzADGroup],ExceptionFullyQualifiedErrorId:Microsoft.Azure.Commands .ActiveDirectory.GetAzureADGroupCommandInvocationInfo:MyCommand:Get-AzADGroupScriptLineNumber:16OffsetInLine
: 14HistoryId: 1ScriptName: C:\home\site\wwwroot\HttpTrigger1\run.ps1Line: $group = Get-AzADGroup -DisplayName $groupADPositionMessage: 在 C:\ps1:1\6www. 14+ $group = Get-AzADGroup -DisplayName $groupAD

在本地創建此 function 時,在我使用 Connect-AzAccount 進行身份驗證后,它可以正常工作。 還嘗試創建一個身份並對其進行身份驗證,但據我所知,它適用於 Azure 資源而不是 AAD。

對於這個問題,這里提供兩種解決方案供大家參考:

1.如果您在Connect-AzAccount命令中使用用戶名/密碼進行身份驗證,則需要確保用戶帳戶具有獲取 AD 組所需的權限。 然后在您的 function 中使用以下代碼:

$User = "{username}"
$PWord = ConvertTo-SecureString -String "{password}" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
Connect-AzAccount -Credential $Credential

$group = Get-AzADGroup -DisplayName "{group name}"

2.如果您不想在 function 中使用用戶名/密碼進行身份驗證。 您可以使用服務主體來執行此操作。

首先你需要在你的 Azure AD 中注冊一個應用程序,我在我的 Azure AD 中注冊了一個名為“ huryGetToken6 ”的應用程序。

然后單擊“證書和機密”選項卡,新建客戶端機密。 將客戶端密碼復制到記事本。 在此處輸入圖像描述

然后將權限添加到注冊的應用程序,按照以下屏幕截圖中的步驟進行操作。 在此處輸入圖像描述

在此處輸入圖像描述

請不要忘記在將權限添加到已注冊的應用程序后單擊“授予 xxx 管理員同意”。

之后,您可以在 function 中使用以下代碼獲取 AD 組:

$username = "{client id/application id}"
$password = "{client secret}"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
Connect-AzAccount -Credential $Credential -Tenant "{tenant id}" -ServicePrincipal

$group = Get-AzADGroup -DisplayName "huryGroup"

對於上述命令中的參數,您可以在您注冊的應用程序的“概述”頁面上找到客戶端 ID/應用程序 ID 和租戶 ID。 在此處輸入圖像描述

暫無
暫無

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

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