如何将用户分配给 Azure Active Directory 中所有可能的组?

How to assign user to all possible groups in Azure Active Directory?

我想向用户添加 Azure Active Directory 中所有可能的组成员身份,但组太多,所以我不想手动添加,是否有任何脚本或按钮可以快速完成此操作?

在 powershell 中试试这个 安装 azure AD 模块

   PS C:\Windows\system32> install-module azuread
   PS C:\Windows\system32> import-module azuread

您可以通过以下方式验证:

PS C:\Windows\system32> get-module azuread

现在将你的 powershell 连接到目录

PS C:\Windows\system32> Connect-AzureAD

它将提示您输入要用于访问您的目录的凭据,并returns确认以显示会话已成功连接到您的目录:

     Account                       Environment Tenant ID
    -------                       ----------- ---------
    xxx@xxxx.onmicrosoft.com      AzureCloud  23b5ff1e-3402-800c-823c-3f…

要从您的目录中检索现有组,请使用 Get-AzureADGroups cmdlet

$groups= get-azureadgroup 
foreach ($group in $groups)
{

Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId <user reference id>
}

替换用户参考 ID,您可以使用 Get-AzureADUser 获取

• 是的,您当然可以通过 powershell 脚本执行此操作,您需要将 Azure AD 中存在的所有组的详细信息导出到 CSV 文件或控制台。然后调用每个组将在powershell命令中指定对象ID的所述用户添加到每个组。请为 Azure AD 中存在的所有组中的指定用户找到下面准备和测试的 powershell 脚本。

Powershell 脚本:-

  Connect-AzureAD
   $groups=Get-AzureADGroup | Select-Object ObjectID
    foreach($group in $groups) {Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId "f08cdf62-6d20-4b65-bdd8-33f84c61802f"} ’

• 结果:-

请查找以下 Microsoft 文档以供参考:-

https://docs.microsoft.com/en-us/azure/active-directory/enterprise-users/groups-settings-v2-cmdlets#add-members

Kartik 和 Vineesh 的答案非常好,但是如果您想获取所有可用的组并且您已经是某些组的成员,则应该使用此脚本

try
{
    Connect-AzureAD
    $groups=Get-AzureADGroup -All $true ` | Select-Object ObjectID
    foreach($group in $groups) {
       $Members = $group | Get-AzureADGroupMember -All $true
       $IsUserInGroup = $Members.ObjectID -contains "your objectId"
       if ($IsUserInGroup -eq $false)
       {
          Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId "your 
          objectId"
       }
    }
}
catch
{
    Write-Error $_.Exception.ToString()
    Read-Host -Prompt "The above error occurred. Press Enter to exit."
}