导出所有 Azure AD 组及其成员 (PowerShell)

Export all Azure AD groups and their members (PowerShell)

我看到过很多关于导出组和获取成员的帖子,但奇怪的是不是两者都有。基本上我需要一个 .csv 文件或类似的每个 AD 组及其相应成员。

我认为这需要从 Azure 完成,因为我们有多个域和 Office 365/incloud 组(Azure AD 列出了所有这些)。

所以我认为没有其他方法可以将我们的组导出到单个 CSV 文件。

此 PowerShell 脚本应该 return 以 CSV 格式提供您要查找的内容(每个 group/member 值一行)。

$groups=Get-AzureADGroup -All $true
ForEach ($group in $groups){
    $members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true
    ForEach ($member in $members){
        Write-output $group.DisplayName "," $member.ObjectId "," $member.ObjectType $member.UserType "," $member.UserPrincipalName >> C:\scripts\output.csv
    }
}

我针对不同的输出格式对 进行了一些微调。

Connect-AzureAD
$groups=Get-AzureADGroup -All $true
$resultsarray =@()
ForEach ($group in $groups){
    $members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true 
    ForEach ($member in $members){
       $UserObject = new-object PSObject
       $UserObject | add-member  -membertype NoteProperty -name "Group Name" -Value $group.DisplayName
       $UserObject | add-member  -membertype NoteProperty -name "Member Name" -Value $member.DisplayName
       $UserObject | add-member  -membertype NoteProperty -name "ObjType" -Value $member.ObjectType
       $UserObject | add-member  -membertype NoteProperty -name "UserType" -Value $member.UserType
       $UserObject | add-member  -membertype NoteProperty -name "UserPrinicpalName" -Value $member.UserPrincipalName
       $resultsarray += $UserObject
    }
}
$resultsarray | Export-Csv -Encoding UTF8  -Delimiter ";" -Path "C:\scripts\output.csv" -NoTypeInformation