Hello Everyone! I’ve got another wild and weird question that I’m looking for help with.
I’ve been tasked to find all of the O365 Groups in my tenant and then provide information on who created them. Simple enough.
My issue comes from when I attempt to provide better info in the Managed By field.
Our UPNs are not very descriptive about who a user might be, so I’m attempting to populate that field instead with their Primary SMTP Address.
This works ok until I run into a user who may not have a mailbox on our tenant. At that point, the script throws an error and writes a blank to the variable. I would love to use the UPN instead of just being blank in those situations.
Here is my original code:
$Groups = Get-UnifiedGroup -ResultSize 10
$Rollup = New-Object System.Collections.ArrayList
$i=0
Foreach ($Group in $Groups) {
$i++
Write-Progress -activity "Gathering O365 Group Info" -status "Group $i of $($Groups.Count)" -percentComplete (($i / $Groups.Count) * 100)
$Array = [PSCustomObject]@{
'Name' = $Group.DisplayName
'Email' = $Group.PrimarySMTPAddress
'Managed By' = $Group.ManagedBy | ForEach-Object {(Get-Recipient $_).PrimarySMTPAddress}
'When Created' = $Group.WhenCreated
'When Changed' = $Group.WhenChanged
}
$Rollup.Add($Array) > $Null
}
$Rollup | Export-Csv 'C:\Users\rmartin\Scripts\O365_Groups.csv' -NoTypeInformation -Encoding UTF8
Here is my feeble attempt at trying a workaround to catch the exception for failing to find the mailbox and just using the original UPN instead.
[string[]]$Groups = Get-Content "C:\Users\rmartin\OneDrive - Illinois Tool Works, Inc\Scripts\content.txt"
$Rollup = New-Object System.Collections.ArrayList
$i=0
Foreach ($Group in $Groups) {
$i++
Write-Progress -activity "Gathering O365 Group Info" -status "Group $i of $($Groups.Count)" -percentComplete (($i / $Groups.Count) * 100)
$group = get-unifiedgroup $group
try
{
$Group.ManagedBy | ForEach-Object {(Get-Recipient -ErrorAction Stop $_).PrimarySMTPAddress}
}
catch
{
$Managedby = $Group.ManagedBy
}
$Array = [PSCustomObject]@{
'Name' = $Group.DisplayName
'Email' = $Group.PrimarySMTPAddress
'Managed By' = $ManagedBy
'When Created' = $Group.WhenCreated
'When Changed' = $Group.WhenChanged
}
$Rollup.Add($Array) > $Null
}
$Rollup | Export-Csv 'C:\Users\rmartin\Scripts\O365_Groups_Blanks.csv' -NoTypeInformation -Encoding UTF8
Any help would be greatly appreciated!
Thanks!