问题
I need help in the powershell script. I am looking to get Azure AD, group membership details for multiple groups which are in the CSV file.
The format, I am looking to get is:
Group Name :SG-Test-Users
Members: xyz, abc etc          
Output needed in this format
Please help
I tried, below script but it is not giving an output in the format I am looking for.
Import-Csv -Path "C:\temp\testgroup.csv" | ForEach-Object {Get-AzureADGroupMember -ObjectId $_.name | select displayname,userprincipalname} | Export-Csv -Path "c:\temp\outputfile1.csv" -NoTypeInformation
Thanks,
回答1:
Try the command below, it works fine on my side. Note it will append data to the file instead of overwriting.
$csv = Import-Csv "C:\Users\joyw\Desktop\testgroup.csv"
foreach ($line in $csv){
    $groupname = $line.GroupName
    $objectid = (Get-AzureADGroup | Where-Object {$_.DisplayName -eq $groupname}).ObjectId
    Get-AzureADGroupMember -ObjectId $objectid | select DisplayName,UserPrincipalName | Export-Csv -Path "C:\Users\joyw\Desktop\outputfile1.csv" -NoTypeInformation -Append
}
My test file:
testgroup.csv
outputfile1.csv
回答2:
I'm new to this, but here's my take
try below (modify output as required) and then save the console output to file and massage as required in excel
cheers
    $gg=Get-AzureADGroup -top 200
    ForEach ($g in $gg){
        $a = Get-AzureADGroup -ObjectId $g.ObjectId  
        $b = Get-AzureADGroupMember -ObjectId $g.ObjectId -All $true 
        ForEach ($c in $b){ 
            Write-host  $a.DisplayName ";" $c.ObjectId ";" $c.ObjectType $c.UserType ";" $c.UserPrincipalName 
        }
    } 
    回答3:
$ResourceAuditArray = @()
ForEach ($g in Get-AzureADGroup -SearchString "<first word of groups e.g. DFC, ADF, DAS>"){ 
    $ResourceAuditArray += Get-AzureADGroupMember -ObjectId $g.ObjectId -All $true  | Select-Object ObjectId, ObjectType, UserType, UserPrincipalName, @{n="DisplayName"; e={$g.DisplayName}}
} 
$ResourceAuditArray | Export-Csv  "<AAD Users list>.Csv"
    来源:https://stackoverflow.com/questions/54192881/export-azure-ad-groups-membership-via-powershell