วิธีใช้ Get-MgGroup ใน PowerShell

Get-MgGroup cmdlet ใน Microsoft Graph PowerShell ดึงรายละเอียดกลุ่มทั้งหมดใน Microsoft Entra ID คุณสามารถรับกลุ่ม Microsoft 365 ทั้งหมดหรือกลุ่มเฉพาะจากองค์กรของคุณ แม้ว่าคุณจะสามารถรับกลุ่มทั้งหมดในศูนย์การจัดการ Microsoft Entra หรือศูนย์การจัดการ Microsoft 365 ได้ แต่คุณก็สามารถทำอะไรได้มากขึ้นและแม่นยำด้วย PowerShell ในบทความนี้ คุณจะได้เรียนรู้วิธีใช้ Get-MgGroup cmdlet ใน PowerShell

ก่อนที่คุณจะเริ่ม

สิ่งสำคัญคือคุณต้องติดตั้งและเชื่อมต่อกับ Microsoft Graph PowerShell ก่อนที่จะดำเนินการต่อไป มิฉะนั้น Get-MgGroup cmdlet จะไม่ทำงาน

ติดตั้ง Microsoft Graph PowerShell

เรียกใช้ PowerShell ในฐานะผู้ดูแลระบบและติดตั้งโมดูล Microsoft Graph PowerShell

Install-Module Microsoft.Graph -Force

สำคัญ:อัปเดตเป็นโมดูล Microsoft Graph PowerShell เวอร์ชันล่าสุดเสมอก่อนที่คุณจะเรียกใช้ cmdlet หรือสคริปต์เพื่อป้องกันข้อผิดพลาดและผลลัพธ์ที่ไม่ถูกต้อง

เชื่อมต่อกับ Microsoft Graph PowerShell

คุณต้องเชื่อมต่อกับ Microsoft Graph PowerShell ด้วยสิทธิ์ที่ถูกต้อง ถ้าคุณไม่ทำ คุณจะไม่สามารถเรียกผลลัพธ์ของกลุ่มด้วย Get-MgGroup cmdlet

Connect-MgGraph -Scopes "Group.Read.All"

คุณต้องการเชื่อมต่อโดยไม่ต้องโต้ตอบกับผู้ใช้เพราะคุณต้องการให้สคริปต์ทำงานโดยอัตโนมัติหรือไม่? ตั้งค่าด้วยการรับรองความถูกต้องตามใบรับรองหรือความลับไคลเอ็นต์ อ่านเพิ่มเติมในบทความเชื่อมต่อกับ Microsoft Graph PowerShell

รับข้อมูลกลุ่ม

มาเริ่มกันที่พื้นฐานกันก่อน เพื่อให้ได้ข้อมูลกลุ่ม

รับข้อมูลกลุ่มเดียว

หากต้องการรับข้อมูลกลุ่มให้ใช้-รหัสกลุ่มพารามิเตอร์และต่อท้าย 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 ในบทความวิธีค้นหากลุ่มว่างใน 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 กัน กรอกเส้นทาง 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 จะเป็นเช่นนี้

อ่านเพิ่มเติมเกี่ยวกับวิธีใช้ Get-MgGroupOwner ใน PowerShell.

รับสมาชิกกลุ่ม

หากต้องการทราบว่าสมาชิกคนใดถูกกำหนดให้กับกลุ่มใด เราสามารถรับรายชื่อกลุ่มทั้งหมดและสมาชิกของพวกเขาได้ คุณต้องใช้ 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 จะเป็นเช่นนี้

อ่านเพิ่มเติมเกี่ยวกับวิธีใช้ Get-MgGroupMember ใน PowerShell.

รับกลุ่มที่ขึ้นต้นด้วยชื่อที่แสดง

เราสามารถเพิ่มเริ่มต้นด้วยโอเปอเรเตอร์และดึงผลลัพธ์ทั้งหมดที่ขึ้นต้นด้วยชื่อที่แสดง

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

วิธีที่ยอดเยี่ยมในการทำความเข้าใจวิธีเพิ่ม Get-MgGroup cmdlet ในสคริปต์ PowerShell คือการดูตัวอย่างสคริปต์ PowerShell:

อ่านเพิ่มเติม:

  • ตรวจสอบว่าสิทธิ์การใช้งานผู้ใช้ Microsoft 365 ได้รับการมอบหมายโดยตรงหรือสืบทอดมาจากกลุ่ม
  • ลบใบอนุญาตที่ได้รับมอบหมายโดยตรงสำหรับผู้ใช้ที่มีใบอนุญาตแบบกลุ่ม
  • ส่งออกสมาชิกกลุ่ม Microsoft 365 เป็น CSV ด้วย PowerShell

บทสรุป

คุณได้เรียนรู้วิธีใช้ Get-MgGroup ใน PowerShell Get-MgGroup cmdlet เป็น cmdlet ที่ยอดเยี่ยมในการดึงข้อมูลกลุ่มจาก Microsoft Entra ID และ Microsoft 365 ใช้พารามิเตอร์เฉพาะหรือรวมเข้าด้วยกันเพื่อกรองผลลัพธ์การค้นหาตามที่คุณต้องการให้ผลลัพธ์ปรากฏ

คุณสนุกกับบทความนี้หรือไม่? คุณอาจต้องการสร้างผู้ใช้ Microsoft Entra ID จาก CSV ด้วย PowerShell อย่าลืมติดตามเราและแบ่งปันบทความนี้

Related Posts