当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 将 Azure AD 组成员导出到 CSV

作者:精品下载站 日期:2024-12-14 09:14:40 浏览:15 分类:玩电脑

使用 PowerShell 将 Azure AD 组成员导出到 CSV


在 Microsoft 365 管理中心中,您可以获取组织中所有组的列表。缺点是只能显示和导出单个组的成员。使用 PowerShell,您可以在一个 CSV 文件中批量导出所有群组及其成员的详细信息。在本文中,你将了解如何使用 PowerShell 将所有 Azure AD 组成员导出到 CSV 文件。

连接到 Microsoft Graph PowerShell

在开始之前,必须安装 Microsoft Graph PowerShell 模块,包括 Microsoft Graph Beta 模块。

运行以下命令来安装 Microsoft Graph 模块。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

您需要使用以下范围连接到 MS Graph。

Connect-MgGraph -Scopes "User.Read.All", "GroupMember.Read.All", "Group.Read.All", "Directory.Read.All"

查看 Azure AD 组信息

我们将展示如何在 PowerShell 输出中显示有关所有 Azure AD 组的信息。请记住,您可以在 PowerShell 控制台上显示的所有信息也可以导出到 CSV 文件。

获取所有 Azure AD 组的列表

您可以显示组织中所有 Azure AD 组的列表。要查看所有组,请使用 Get-MgGroup cmdlet。

Get-MgGroup -All

PowerShell 输出显示以下结果。

PS C:\> Get-MgGroup -All

DisplayName     Id                                   MailNickname    Description             GroupTypes
-----------     --                                   ------------    -----------             ----------
Mail security   00d760d0-d5a7-49d8-88d5-21a093b34bd7 Mail.Security   Mail security           {}        
Sales Spain     06a56975-ba05-43cc-8a2a-e8879f42e622 SalesSpain                              {}        
Sales UK        0a33cb80-41ff-4e92-8c85-269a81dec110 SalesUK                                 {}        
Sales Sweden    0c3107c9-b1e7-4a0b-ac50-38ef25e6aeb2 SalesSweden                             {}        
Management Team 18233557-5240-4a0f-80b5-ccacf8c97102 ManagementTeam  For the Management Team {Unified} 
Sales Team      2de06857-1e0c-4ffe-b85c-7b0c18bb71d3 SalesTeam       For the Sales Team      {Unified} 
Finance USA     38fc2d7f-b496-47fc-9fa8-96178d17e52e FinanceUSA                              {}        
Sales Employees 525672db-4320-40e5-8a42-69457dae2347 Sales.Employees Sales Employees         {}        

使用显示名称获取 Azure AD 组详细信息

在我们的示例中,我们想要查找组 Sales UK 的详细信息。如果您知道群组的显示名称,则可以找到群组 ID 号

运行以下 PowerShell 命令示例。

Get-MgGroup -Filter "DisplayName eq 'Sales UK'" | fl Id, DisplayName, Description, GroupTypes

PowerShell 输出显示以下结果。

PS C:\> Get-MgGroup -Filter "DisplayName eq 'Sales UK'" | fl Id, DisplayName, Description, GroupTypes

Id          : 0a33cb80-41ff-4e92-8c85-269a81dec110
DisplayName : Sales UK
Description : 
GroupTypes  : {}

显示 Azure AD 组成员详细信息

要获取有关特定组成员的扩展信息,我们需要使用 PowerShell cmdlet Get-MgGroupMember。

计算 Azure AD 组成员总数

如果您知道群组 ID 号,您就可以了解特定群组有多少成员。请参阅上一步以获取特定组的组 ID 号

在我们的示例中,我们将使用参数 -GroupId 来计算组 Sales UK 的成员数量。

运行以下 PowerShell 命令示例。

(Get-MgGroupMember -GroupId '0a33cb80-41ff-4e92-8c85-269a81dec110' -All).Count

PowerShell 输出显示组成员的数量。

PS C:\> (Get-MgGroupMember -GroupId '0a33cb80-41ff-4e92-8c85-269a81dec110' -All).Count
11

在我们的示例中,PowerShell 输出显示组 Sales UK 有 11 名成员。

使用 GroupId 获取特定组的 Azure AD 成员

您还可以使用 -GroupId 参数获取单个组的成员。在我们的示例中,我们希望查看哪些成员属于组 Sales UK

请参阅下面的 PowerShell 语法。

Get-MgGroupMember -GroupId '<Group Id number>' -All

运行以下 PowerShell 命令示例。

Get-MgGroupMember -GroupId '0a33cb80-41ff-4e92-8c85-269a81dec110' -All

PowerShell输出结果仅显示Sales UK所有群组成员的ID号

PS C:\> Get-MgGroupMember -GroupId '0a33cb80-41ff-4e92-8c85-269a81dec110' -All

Id                                   DeletedDateTime
--                                   ---------------
eec2668a-0773-4947-93ba-2223f6acfe55                
fd199cb4-2ebf-4171-96e2-12fd75453e39                
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b                
d89be5ce-6495-4009-b61b-81126c239c34                
a9532b30-4edb-4b66-a3b0-6ac972a6065b                
b602b148-2fcf-435a-9d34-ce72c3a8c748                
3bb176aa-d0ba-47f7-aecc-f4837593006e                
1d9fc432-6a9f-44c5-8cda-0291662bc825                
12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b                
274d72d7-cc30-4a64-bc33-c99ff96c3abf                
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5                

如果您需要更详细的信息,例如每个成员的 UserPrincipalNameDisplayName,请阅读下一步。

获取特定组的 Azure AD 组成员详细信息

要获取有关特定组成员的更多详细信息,您可以使用以下 PowerShell 脚本。

该脚本将对结果进行排序并显示有关成员 UserPrincipalNameDisplayNameAlias 的以下信息。

在我们的示例中,我们将使用相同的 GroupId 并将其粘贴到第 5 行。

运行以下 PowerShell 脚本。

# Connect to Microsoft Graph with specified scopes
Connect-MgGraph -Scopes "User.Read.All", "GroupMember.Read.All", "Group.Read.All", "Directory.Read.All"

# GroupId
$groupId = '0a33cb80-41ff-4e92-8c85-269a81dec110'

# Initialize an array to store the output
$outputArray = @()

try {
    # Get members of the group
    $members = Get-MgGroupMember -GroupId $groupId -All

    # Iterate through the members and retrieve UPN, display name, and alias
    foreach ($member in $members) {
        $userId = $member.Id
        try {
            $user = Get-MgUser -UserId $userId -ErrorAction SilentlyContinue

            if ($user) {
                $userPrincipalName = $user.UserPrincipalName
                $displayName = $user.DisplayName
                # Extract alias from UserPrincipalName
                $alias = $userPrincipalName -replace "@.*"
            }
            else {
                # User not found
                $userPrincipalName = 'Not Available'
                $displayName = 'Not Available'
                $alias = 'Not Available'
            }

            $output = [PSCustomObject]@{
                'Id'                = $userId
                'UserPrincipalName' = $userPrincipalName
                'DisplayName'       = $displayName
                'Alias'             = $alias
            }

            $outputArray += $output
        }
        catch {
            # Handle any errors while fetching user details
            Write-Host "Error fetching user details for UserId: $userId - $_" -ForegroundColor Red
        }
    }
}
catch {
    # Handle any errors while fetching group members
    Write-Host "Error fetching group members: $_" -ForegroundColor Red
}

# Display the output in a formatted table
$outputArray | Format-Table -Property Id, UserPrincipalName, DisplayName, Alias

PowerShell 输出显示组成员IdUserPrincipalName显示名称

Id                                   UserPrincipalName          DisplayName   Alias        
--                                   -----------------          -----------   -----        
eec2668a-0773-4947-93ba-2223f6acfe55 [email protected]    David Kent    David.Kent   
fd199cb4-2ebf-4171-96e2-12fd75453e39 [email protected]   Susan Brown   Susan.Brown  
fa956d8c-87df-4cd4-ac2a-ac1f3d7cac8b [email protected]   Chris Lucas   Chris.Lucas  
d89be5ce-6495-4009-b61b-81126c239c34 [email protected] George Wilson George.Wilson
a9532b30-4edb-4b66-a3b0-6ac972a6065b [email protected]    Jill Bates    Jill.Bates   
b602b148-2fcf-435a-9d34-ce72c3a8c748 [email protected]   Diana Baker   Diana.Baker  
3bb176aa-d0ba-47f7-aecc-f4837593006e [email protected]    Mary James    Mary.James   
1d9fc432-6a9f-44c5-8cda-0291662bc825 [email protected]   Ricky Lewis   Ricky.Lewis  
12eefbb2-e5f4-4eec-bd18-df7ca2f1ee6b [email protected]    Ken Walker    Ken.Walker   
274d72d7-cc30-4a64-bc33-c99ff96c3abf [email protected]     RoomTest8     RoomTest8    
41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5 [email protected] Amanda Hansen Amanda.Hansen

在下一步中,您可以获得相同的成员详细信息,但不使用组 ID。

将特定组的 Azure AD 成员导出到 CSV

将特定组的所有成员导出到单个 CSV 文件,而不使用组 ID 号。假设您不知道特定组的 ID 号或者您不想检索它。在我们的示例中,我们希望获取组 Sales UK 的所有成员详细信息。

  1. 创建一个名为 temp 的文件夹,并将其保存在 (C:) 驱动器中(如果没有)
  2. 在第 5 行中输入GroupName
  3. 运行以下 PowerShell 脚本
# Connect to Microsoft Graph with specified scopes
Connect-MgGraph -Scopes "User.Read.All", "GroupMember.Read.All", "Group.Read.All", "Directory.Read.All"

# Specify the group name to retrieve
$GROUP_NAME = "Sales UK"

# Retrieve the group based on the specified display name
$group = Get-MgGroup -Filter "DisplayName eq '$GROUP_NAME'"

# Check if the group was found
if ($group -eq $null) {
    Write-Host "Group with name '$GROUP_NAME' not found."
}
else {
    # Check if the Group ID is not empty or null
    if ([string]::IsNullOrWhiteSpace($group.id)) {
        Write-Host "Group ID for '$GROUP_NAME' is empty or null. Cannot retrieve group members."
    }
    else {
        # Retrieve group members using the valid Group ID
        $members = Get-MgGroupMember -GroupId $group.id -All

        # Initialize an array to store user information
        $users = @()

        # Iterate through each group member and retrieve user details
        foreach ($member in $members) {
            $user = Get-MgUser -UserId $member.Id -ErrorAction SilentlyContinue

            # Check if $user is not null before accessing properties
            if ($user -ne $null) {
                # Extract the alias from the UserPrincipalName
                $alias = $user.UserPrincipalName.Split("@")[0]

                # Add user information to the array
                $users += New-Object PSObject -Property @{
                    Group             = $group.DisplayName; 
                    Name              = $user.DisplayName; 
                    UserPrincipalName = $user.Mail;
                    Alias             = $alias
                }
            }
        }
        # Export user information to a CSV file
        $users | Export-Csv "C:\temp\GroupMembers.csv" -NoTypeInformation -Encoding UTF8
    }
}
  1. 转到 C:\temp 文件夹中的 CSV 文件
  2. 使用 Microsoft Excel 等应用程序打开 CSV 文件以查看结果

[玩转系统] 使用 PowerShell 将 Azure AD 组成员导出到 CSV

  1. CSV 文件显示 Sales UK 组中所有成员的别名姓名UserPrincipalName

[玩转系统] 使用 PowerShell 将 Azure AD 组成员导出到 CSV

将 Azure AD 组成员批量导出到 CSV

导出组织中可用的所有 Azure AD 组,包括其组成员。

此 PowerShell 脚本创建一个 CSV 文件并添加组织中的所有群组,其中包含每个成员的别名名称UserPrincipalName

# Connect to Microsoft Graph with specified scopes
Connect-MgGraph -Scopes "User.Read.All", "GroupMember.Read.All", "Group.Read.All", "Directory.Read.All"

# Retrieve all groups
$groups = Get-MgGroup -All

# Initialize an array to store user information
$allUsers = @()

# Iterate through each group and retrieve group members
foreach ($group in $groups) {
    if ([string]::IsNullOrWhiteSpace($group.id)) {
        Write-Host "Group ID for group '$($group.DisplayName)' is empty or null. Cannot retrieve group members."
    }
    else {
        # Retrieve group members using the valid Group ID
        $members = Get-MgGroupMember -GroupId $group.id -All

        # Iterate through each group member and retrieve user details
        foreach ($member in $members) {
            $user = Get-MgUser -UserId $member.Id -ErrorAction SilentlyContinue

            # Check if $user is not null before accessing properties
            if ($user -ne $null) {
                # Extract the alias from the UserPrincipalName
                $alias = $user.UserPrincipalName.Split("@")[0]

                # Add user information to the array
                $allUsers += New-Object PSObject -Property @{
                    Group             = $group.DisplayName; 
                    Name              = $user.DisplayName; 
                    UserPrincipalName = $user.Mail;
                    Alias             = $alias
                }
            }
        }
    }
}

# Export all user information to a CSV file
$allUsers | Export-Csv "C:\temp\AllGroupMembers.csv" -NoTypeInformation -Encoding UTF8

转到 C:\temp 文件夹中的 CSV 文件。使用 Microsoft Excel 等应用程序打开 CSV 文件以查看结果。

[玩转系统] 使用 PowerShell 将 Azure AD 组成员导出到 CSV

下图显示了一个 CSV 文件,其中包含所有可用的 Azure AD 组,包括成员的身份。

[玩转系统] 使用 PowerShell 将 Azure AD 组成员导出到 CSV

就是这样!

了解更多:为 Azure AD 用户设置员工 ID »

结论

您了解了如何使用 PowerShell 将 Azure AD 组成员导出到 CSV 文件。与 Microsoft Entra 管理中心相比,Microsoft Graph PowerShell 有很多用于查看和导出 Azure 组成员的选项。一个很好的方法是,您可以将组织中的所有 Azure AD 组和成员身份批量导出到单个 CSV 文件。

您喜欢这篇文章吗?您可能还喜欢使用 Microsoft Graph PowerShell 发送电子邮件。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯