[玩转系统] 如何在 PowerShell 中使用 Get-MgGroup
作者:精品下载站 日期:2024-12-14 01:57:26 浏览:14 分类:玩电脑
如何在 PowerShell 中使用 Get-MgGroup
Microsoft Graph PowerShell 中的 Get-MgGroup cmdlet 检索 Microsoft Entra ID 中的所有组详细信息。您可以获取组织中的所有 Microsoft 365 组或特定组。虽然您可以获取 Microsoft Entra 管理中心或 Microsoft 365 管理中心中的所有组,但您始终可以使用 PowerShell 执行更多操作并保持精确。在本文中,您将了解如何在 PowerShell 中使用 Get-MgGroup cmdlet。
开始之前
在继续操作之前,请务必安装并连接到 Microsoft Graph PowerShell。否则,Get-MgGroup cmdlet 将不起作用。
安装 Microsoft Graph PowerShell
以管理员身份运行 PowerShell 并安装 Microsoft Graph PowerShell 模块。
Install-Module Microsoft.Graph -Force
重要提示:在运行 cmdlet 或脚本之前,请务必更新到最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。
连接到 Microsoft Graph PowerShell
您需要使用正确的权限连接到 Microsoft Graph PowerShell。如果不这样做,则无法使用 Get-MgGroup cmdlet 检索组结果。
Connect-MgGraph -Scopes "Group.Read.All"
您是否希望在没有用户交互的情况下进行连接,因为您希望脚本自动运行?使用基于证书的身份验证或客户端密钥进行设置。请阅读连接到 Microsoft Graph PowerShell 一文来了解更多信息。
获取群组信息
我们首先从基础开始,那就是获取群组信息。
获取单组信息
要获取组信息,请使用-GroupId参数并附加组ID。
Get-MgGroup -GroupId "14603c19-0afa-4f2f-9c13-64d25eedfca3"
您还可以使用-Filter参数搜索DisplayName以获取组信息。
Get-MgGroup -Filter "DisplayName eq 'Group1'"
添加 Format-List cmdlet 以获取属性列表。
Get-MgGroup -Filter "DisplayName eq 'Group1'" | Format-List
获取所有群组信息
运行 Get-MgGroup cmdlet(包括 -All 参数)以检索所有组。
注意:始终使用-All 参数来获取所有结果。否则,只会出现 100 个项目。
Get-MgGroup -All
为了计算所有组的数量,我们将 Measure-Object cmdlet 添加到命令中。
Get-MgGroup -All | Measure-Object | Select-Object -ExpandProperty Count
另一种计算所有组的方法。
(Get-MgGroup -All).Count
获取空组
查找所有没有分配成员的组。
Get-MgGroup -All | Where-Object { (Get-MgGroupMember -GroupId $_.Id).Count -eq 0 } | Sort-Object DisplayName
若要获取所有组及其组类型,请使用如何使用 PowerShell 在 Microsoft 365 中查找空组一文中的 PowerShell 脚本。
获取所有云组
如果您有混合环境,组将从本地 AD 同步到 Microsoft Entra ID。本地 AD 是您的域权限,您应该在那里创建组。但是,组织中可能存在直接在云中创建的组。
让我们仅过滤云组并按显示名称对它们进行排序。
Get-MgGroup -All -Filter "OnPremisesSyncEnabled ne true" -ConsistencyLevel eventual -CountVariable CountVar | Sort-Object DisplayName
获取本地同步组
仅获取同步到云的本地组并按显示名称对它们进行排序。
Get-MgGroup -All -Filter "OnPremisesSyncEnabled eq true" -ConsistencyLevel eventual -CountVariable CountVar | Sort-Object DisplayName
获取群组的所有者
要了解哪个所有者分配给哪个组,我们可以获取所有组及其所有者的列表。您必须使用Get-MgGroup 和Get-MgGroupOwner cmdlet 来检索成员。
让我们将所有组(包括其所有者)导出到 CSV 文件。在第 4 行填写 CSV 路径。
Connect-MgGraph -Scopes "Group.Read.All", "Directory.Read.All" -NoWelcome
# Define the CSV file path
$csvPath = "C:\temp\GroupOwners.csv"
# Get all groups
$Groups = Get-MgGroup -All
$TotalGroups = $Groups.Count
$ProgressCounter = 0
$Report = [System.Collections.Generic.List[Object]]::new()
# Loop through each group
foreach ($Group in $Groups) {
$ProgressCounter++
$Percentage = [Math]::Round(($ProgressCounter / $TotalGroups) * 100, 2)
Write-Progress -Activity "Processing Groups - $Percentage%" -Status "Checking owners for $($Group.DisplayName)" -PercentComplete $Percentage
$Owners = Get-MgGroupOwner -GroupId $Group.Id -All | Select-Object -ExpandProperty AdditionalProperties
if ($Owners) {
foreach ($Owner in $Owners) {
$OwnerType = switch ($Owner.'@odata.type') {
"#microsoft.graph.user" { "User" }
"#microsoft.graph.group" { "Group" }
"#microsoft.graph.servicePrincipal" { "Service Principal" }
"#microsoft.graph.device" { "Device" }
"#microsoft.graph.orgContact" { "Contact" }
default { "Unknown" }
}
$ReportLine = [PSCustomObject]@{
GroupName = $Group.DisplayName
OwnerName = $Owner.displayName
OwnerEmail = $Owner.mail
OwnerUserPrincipalName = $Owner.userPrincipalName
OwnerType = $OwnerType
}
$Report.Add($ReportLine)
}
}
else {
$ReportLine = [PSCustomObject]@{
GroupName = $Group.DisplayName
OwnerName = ""
OwnerEmail = ""
OwnerUserPrincipalName = ""
OwnerType = ""
}
$Report.Add($ReportLine)
}
}
# Complete the progress bar
Write-Progress -Activity "Processing Groups" -Status "Completed" -PercentComplete 100 -Completed
# Export the report to CSV
$Report | Sort-Object GroupName | Export-Csv -Path $csvPath -NoTypeInformation -Encoding utf8
Write-Host "Report exported to $csvPath" -ForegroundColor Cyan
这就是 CSV 文件报告的样子。
阅读有关如何在 PowerShell 中使用 Get-MgGroupOwner 的更多信息。
获取群组成员
要知道哪个成员被分配到哪个组,我们可以获取所有组及其成员的列表。您必须使用Get-MgGroup 和Get-MgGroupMember cmdlet 来检索成员。
让我们将所有组(包括其成员)导出到 CSV 文件。在第 4 行填写 CSV 路径。
Connect-MgGraph -Scopes "Group.Read.All", "Directory.Read.All" -NoWelcome
# Define the CSV file path
$csvPath = "C:\temp\GroupMembers.csv"
# Get all groups
$Groups = Get-MgGroup -All
$TotalGroups = $Groups.Count
$ProgressCounter = 0
$Report = [System.Collections.Generic.List[Object]]::new()
# Loop through each group
foreach ($Group in $Groups) {
$ProgressCounter++
$Percentage = [Math]::Round(($ProgressCounter / $TotalGroups) * 100, 2)
Write-Progress -Activity "Processing Groups - $Percentage%" -Status "Checking members for $($Group.DisplayName)" -PercentComplete $Percentage
$Members = Get-MgGroupMember -GroupId $Group.Id -All | Select-Object -ExpandProperty AdditionalProperties
if ($Members) {
foreach ($Member in $Members) {
$memberType = switch ($Member.'@odata.type') {
"#microsoft.graph.user" { "User" }
"#microsoft.graph.group" { "Group" }
"#microsoft.graph.servicePrincipal" { "Service Principal" }
"#microsoft.graph.device" { "Device" }
"#microsoft.graph.orgContact" { "Contact" }
default { "Unknown" }
}
$ReportLine = [PSCustomObject]@{
GroupName = $Group.DisplayName
MemberName = $Member.displayName
MemberEmail = $Member.mail
MemberUserPrincipalName = $Member.userPrincipalName
MemberType = $memberType
}
$Report.Add($ReportLine)
}
}
else {
$ReportLine = [PSCustomObject]@{
GroupName = $Group.DisplayName
MemberName = ""
MemberEmail = ""
MemberUserPrincipalName = ""
MemberType = ""
}
$Report.Add($ReportLine)
}
}
# Complete the progress bar
Write-Progress -Activity "Processing Groups" -Status "Completed" -PercentComplete 100 -Completed
# Export the report to CSV
$Report | Sort-Object GroupName | Export-Csv -Path $csvPath -NoTypeInformation -Encoding utf8
Write-Host "Report exported to $csvPath" -ForegroundColor Cyan
这就是 CSV 文件报告的样子。
阅读有关如何在 PowerShell 中使用 Get-MgGroupMember 的更多信息。
获取以显示名称开头的组
我们可以添加 startsWith 运算符并检索以显示名称开头的所有结果。
Get-MgGroup -All -Filter "startsWith(DisplayName,'Sales')"
您还可以添加字母而不是单词。
Get-MgGroup -All -Filter "startsWith(DisplayName,'S')"
获取以邮件地址结尾的组
添加 endsWith 运算符以检索以特定邮件地址结尾的所有组,并按显示名称对它们进行排序。
Get-MgGroup -All -Filter "endsWith(mail,'exoip.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar
搜索以特定邮件地址结尾的两个域。
Get-MgGroup -All -Filter "endsWith(mail,'exoip.com') or endsWith(mail,'tajran.com')" -Sort "displayName" -ConsistencyLevel eventual -CountVariable CountVar
获取创建日期的群组
获取单个组的创建日期。
Get-MgGroup -GroupId "14603c19-0afa-4f2f-9c13-64d25eedfca3" | Select-Object DisplayName, CreatedDateTime
获取所有组的创建日期。
Get-MgGroup -All | Select-Object DisplayName, CreatedDateTime | Sort-Object CreatedDateTime
让我们获取 2024 年创建的所有组。
Get-MgGroup -All -Filter ("CreatedDateTime ge " + (Get-Date "2024-01-01T00:00:00Z").ToString("yyyy-MM-ddTHH:mm:ssZ") + " and CreatedDateTime le " + (Get-Date "2024-12-31T23:59:59Z").ToString("yyyy-MM-ddTHH:mm:ssZ")) -ConsistencyLevel eventual -CountVariable Count
Get-MgGroup PowerShell 脚本示例
了解如何在 PowerShell 脚本中添加 Get-MgGroup cmdlet 的一个很好的方法是查看 PowerShell 脚本示例:
- 检查 Microsoft 365 用户许可证是直接分配还是从组继承
- 删除为具有组许可证的用户直接分配的许可证
- 使用 PowerShell 将 Microsoft 365 组成员导出到 CSV
结论
您学习了如何在 PowerShell 中使用 Get-MgGroup。 Get-MgGroup cmdlet 是一个出色的 cmdlet,用于从 Microsoft Entra ID 和 Microsoft 365 检索组。使用特定参数或将它们组合起来,根据您希望结果的显示方式筛选搜索结果。
您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 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