[玩转系统] 使用 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
如果您需要更详细的信息,例如每个成员的 UserPrincipalName 或 DisplayName,请阅读下一步。
获取特定组的 Azure AD 组成员详细信息
要获取有关特定组成员的更多详细信息,您可以使用以下 PowerShell 脚本。
该脚本将对结果进行排序并显示有关成员 UserPrincipalName、DisplayName 和 Alias 的以下信息。
在我们的示例中,我们将使用相同的 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 输出显示组成员Id、UserPrincipalName 和显示名称。
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 的所有成员详细信息。
- 创建一个名为 temp 的文件夹,并将其保存在 (C:) 驱动器中(如果没有)
- 在第 5 行中输入GroupName
- 运行以下 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
}
}
- 转到 C:\temp 文件夹中的 CSV 文件
- 使用 Microsoft Excel 等应用程序打开 CSV 文件以查看结果
- CSV 文件显示 Sales UK 组中所有成员的别名、姓名和UserPrincipalName。
将 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 文件以查看结果。
下图显示了一个 CSV 文件,其中包含所有可用的 Azure AD 组,包括成员的身份。
就是这样!
了解更多:为 Azure AD 用户设置员工 ID »
结论
您了解了如何使用 PowerShell 将 Azure AD 组成员导出到 CSV 文件。与 Microsoft Entra 管理中心相比,Microsoft Graph PowerShell 有很多用于查看和导出 Azure 组成员的选项。一个很好的方法是,您可以将组织中的所有 Azure AD 组和成员身份批量导出到单个 CSV 文件。
您喜欢这篇文章吗?您可能还喜欢使用 Microsoft Graph PowerShell 发送电子邮件。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag