Export all Azure AD Groups and their members (Powershell)

我们两清 提交于 2020-07-10 09:24:08

问题


I have seen many threads on exporting groups and getting members, but strangely not both. Basically I need a .csv file or similar with every AD group and it's corresponding members.

I believe it needs to be done from Azure as we have multiple domains and Office 365/incloud groups (Azure AD has all of these listed).

So I don't think there is another way to export our groups to a single csv file.


回答1:


This PowerShell should return you what you are looking for in csv format (one line for each group/member value).

$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
    }
} 



回答2:


Thank You. I have tweaked it a little for a different output format.

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


来源:https://stackoverflow.com/questions/58828015/export-all-azure-ad-groups-and-their-members-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!