I have a very simple script that is connecting to AzureAD and pulling Roles and Members of said roles within AD.
I can get it to run fine within powershell, however when i start to add Out-File or similar i obvioulsy need to call from a stored item, I add $Export to the foreach, it runs fine however when i preview it, it is missing the $Write-Host $ADRole.Displaname from the foreach.
## CONNECT AZURE AD ##
Connect-AzureAD
## Get Administrator Account Users
$Export = ForEach ($ADRole in Get-AzureADDirectoryRole) {
Write-Host $ADRole.DisplayName
Get-AzureADDirectoryRoleMember -ObjectId $ADRole.objectID | Format-Table DisplayName,UserPrincipalName,UserType,ObjectId
}
$Export | Out-File -FilePath C:\Temp\Test.txt
As per my comment below i think the output of this doesnt work as efficient as it should as there is no specific way to filter by user etc, which ofcourse is a limitation with a .txt file so i think i need to make this output to a csv.
Upon initial output of CSV it would output all as 1 column and 1 line so not very functional, give the $ADRole.DisplayName is shown above the users i think this would be best to show inline for the CSV so it would output as follows.
$ADRole.DisplayName, $Member.DisplayName,$Member.UserPrincipalName,$Member.UserType,$Member.ObjectID
Although there are roles which have no users assigned to it, however those roles still need to show for any external audits?
What is the best way to achieve this?