【问题标题】:View groups for every user in Azure AD with powershell使用 powershell 查看 Azure AD 中每个用户的组
【发布时间】:2021-10-14 13:54:49
【问题描述】:

正如标题所说。我正在寻找一种方法来列出每个用户以及他们所在的组。

我知道您可以如何使用Get-AzureADGroupMember -ObjectId "groupidhere" 然后输出是该组中的所有用户。但是你将如何自动化呢?这甚至可能与powershell有关吗?

在这之后我将使用这个表在 Hudu 中创建一个表。不过,我还没有看到有人与组和用户一起这样做,所以据我所知,这是不可能或不应该的。

所以我从$Users 得到的输出也显示了来自$Groups_Name 的一些输出

一张表格,其中包含有关用户的所有信息,以及他们所在的组。

|姓名 |电子邮件 |集团 |

所以输出会是这样的:

DisplayName     UserPrincipalName     DisplayName
-----------     -----------------     -----------
Name Nameson     user@domain.com       Group names 
Name Nameson     user@domain.com       Group names
Name Nameson     user@domain.com       Group names
Name Nameson     user@domain.com       Group names
Name Nameson     user@domain.com       Group names
Name Nameson     user@domain.com       Group names
Name Nameson     user@domain.com       Group names
Name Nameson     user@domain.com       Group names

正在编写的脚本(我知道这非常混乱)

# Table of all users
$Users = Get-AzureADUser -All:$true

# Table of all groups
$Groups = Get-AzureADGroup


# ALL users ObjectId
$Users_ObjectId = $Users | Select-Object ObjectId

# ALL Groups ObjectId
$Groups_ObjectId = $Groups | Select-Object ObjectId

#Group names - list
$Groups_Name = $Groups | Select-Object DisplayName

#User names - list
$Users_Name = $Users | Select-Object DisplayName

foreach ($i in $Users ) {

    # If
    if ($Groups -contains $Users_ObjectId) {

        #print a table with desired formatting
        #$Users $Groups_Name 
    }
}

【问题讨论】:

  • 只是一个建议:您可以使用 ($Users.ObjectId) 而不是 ($Users_ObjectId = $Users | Select-Object ObjectId)

标签: azure powershell azure-active-directory


【解决方案1】:

尝试像这样使用Get-AzureADUserMembership

$users = Get-AzureADUser -All $true

$report = Foreach ($user in $users) {
  $groups = $user | Get-AzureADUserMembership

  # create output objects with username and groups:
  Foreach ($group in $groups) {
    [PSCustomObject][ordered]@{ 
      UserDisplayName   = $user.DisplayName
      UserPrincipalName = $user.UserPrincipalName
      GroupDisplayName  = $group.DisplayName
}}}

# print a table with desired formatting
$report | ft

报告看起来像这样:

UserDisplayName UserPrincipalName  GroupDisplayName                            
--------------- -----------------  ----------------                            
John Smith      j.smith@domain.com Marketing
John Smith      j.smith@domain.com Marketing-VIPs
John Doe        j.doe@domain.com   Sales                           
John Doe        j.doe@domain.com   Management  

【讨论】:

  • 谢谢队长!这对我有很大帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 2021-09-23
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多