如何在 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 一文来了解更多信息。
获取群组信息
我们首先从基础开始,那就是获取群组信息。
获取单组信息
要获取组信息,请使用-组ID参数并附加组 ID。
Get-MgGroup -GroupId "14603c19-0afa-4f2f-9c13-64d25eedfca3"
您还可以使用-筛选参数来搜索 DisplayName 以获取组信息。
Get-MgGroup -Filter "DisplayName eq 'Group1'"
添加格式列表cmdlet 获取属性列表。
Get-MgGroup -Filter "DisplayName eq 'Group1'" | Format-List
获取所有群组信息
运行 Get-MgGroup cmdlet,包括-全部参数,检索所有组。
笔记:始终使用-全部参数来获取所有结果。否则,只会出现 100 个项目。
Get-MgGroup -All
为了计算所有组的数量,我们将添加测量对象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 脚本如何使用 PowerShell 在 Microsoft 365 中查找空组。
获取所有云组
如果您有混合环境,组将从本地 AD 同步到 Microsoft Entra ID。本地 AD 是您的域权限,您应该在那里创建组。但是,组织中可能存在直接在云中创建的组。
我们仅过滤云组并按显示名称对它们进行排序。
阅读更多:修复:开始菜单中缺少 Windows PowerShell
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 文件。填写 CSV 路径4号线。
Connect-MgGraph -Scopes "Group.Read.All", "Directory.Read.All" -NoWelcome
# Define the CSV file path
$csvPath = "C:tempGroupOwners.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 文件。填写 CSV 路径4号线。
Connect-MgGraph -Scopes "Group.Read.All", "Directory.Read.All" -NoWelcome
# Define the CSV file path
$csvPath = "C:tempGroupMembers.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。
获取以显示名称开头的组
我们可以添加开始于运算符并检索以显示名称开头的所有结果。
Get-MgGroup -All -Filter "startsWith(DisplayName,'Sales')"
您还可以添加字母而不是单词。
Get-MgGroup -All -Filter "startsWith(DisplayName,'S')"
获取以邮件地址结尾的组
添加结束于运算符检索以特定邮件地址结尾的所有组并按显示名称对它们进行排序。
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
了解如何在 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 用户。不要忘记关注我们并分享这篇文章。
