Last active
April 8, 2020 10:51
-
-
Save RichieBzzzt/736a0f967b8be4963e835c58dfb1082b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ($null -eq (Get-AzContext)) { | |
Write-Host "No Azure Context... logging in." | |
Login-AzAccount -UseDeviceAuthentication | |
} | |
$groups = @("bob","tim" | |
) | |
$outputPath = "" | |
Function Export-UserDetailsFromGroups { | |
param( | |
$affectedGroups, | |
$outputPath, | |
$processedGroups = @() | |
) | |
$i = 1 | |
foreach ($affectedGroup in $affectedGroups) { | |
Write-Host "Getting group members and emails for $affectedGroup" | |
$i++ | |
if (($processedGroups.Contains($affectedGroup)) -eq $false) { | |
$processedGroups += $affectedGroup | |
$groupMembers = Get-AADGroupMembers -GroupName $affectedGroup | |
foreach ($groupMember in $groupMembers) { | |
if ($groupMember.Type -eq "User") { | |
Set-UserDetails -user $groupMember -outputPath $outputPath | |
} | |
if ($GroupMember.Type -eq "Group") { | |
Export-UserDetailsFromGroups -affectedGroups $GroupMember.DisplayName -outputPath $outputPath -processedGroups $processedGroups | |
} | |
} | |
} | |
} | |
} | |
Function Get-AADGroupMembers { | |
param ( | |
$GroupName | |
) | |
$groupMembers = Get-AzADGroupMember -GroupDisplayName $GroupName | |
return $groupMembers | |
} | |
Function Set-UserDetails { | |
param( | |
$user, | |
$outputPath | |
) | |
$csvStreamWriter.WriteLine(" $($User.Id), $($User.UserPrincipalName), $($User.DisplayName)") | |
} | |
$csvStreamWriter = New-Object IO.StreamWriter "C:\Temp\userdetails.csv" | |
$csvStreamWriter.WriteLine("Id, UserPrincipalName, DisplayName") | |
Measure-Command { Export-UserDetailsFromGroups -affectedGroups $groups -outputPath $csvStreamWriter } | |
$csvStreamWriter.Dispose() | |
Import-Csv C:\Temp\userdetails.csv | Sort-Object Id, DisplayName, UserPrincipalName -Unique | Export-Csv -Force -Path "C:\Temp\uniqueuserdetails.csv" -NoTypeInformation | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment