-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrossTenantObjectExport.ps1
executable file
·604 lines (538 loc) · 23.7 KB
/
CrossTenantObjectExport.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<#
.Synopsis
Exports object information for cross tenant migrations
.Description
This script exports object information from AD, EXO and AzureAD. It can be used to recreate users in a target domain for a cross tenant migration.
.Parameter GroupName
The name of the group that contains all the objects that you want to export.
.Parameter DomainName
The AD domain name. This is used for the Get-ADUser command. This can also be a specific Domain controller.
.Example
.\CrossTenantObjectExport.ps1 -GroupName "Migration Users" -DomainName ad.domain
This runs the basic user export for the users that are a member of the group "Migration Users" .
.Example
.\CrossTenantObjectExport.ps1 -GroupName "Migration Users" -DomainName ad.domain -IncludeInboxRules
This runs the basic export and includes the inbox rules for each user (This requires Exchange Admin)
.Example
.\CrossTenantObjectExport.ps1 -GroupName "Migration Users" -DomainName ad.domain -IncludeMailboxStats
This runs the basic export and includes the mailbox and folder statistics for each user (This requires Exchange Admin)
.Example
.\CrossTenantObjectExport.ps1 -GroupName "Migration Users" -DomainName ad.domain -IncludeMailboxPermissions
This runs the basic export and includes the mailbox permissions for each user (This requires Exchange Admin)
.Example
.\CrossTenantObjectExport.ps1 -GroupName "Migration Users" -DomainName ad.domain -IncludeInboxRules -IncludeMailboxStats -IncludeMailboxPermissions
This runs the complete export (This requires Exchange Admin)
.Notes
#######################################################
# . . #
# . Written By: . #
#.....................................................#
# . Jeffrey Kraemer . #
# . ENS, Inc. . #
# . [email protected] . #
#.....................................................#
# . . #
#######################################################
#>
#Requires -Modules ExchangeOnlineManagement, AzureAD, MSOnline
[CmdletBinding()]
Param(
# Cross tenant group name
[Parameter(Mandatory = $True)]
[string]
$GroupName,
# AD domain
[Parameter(Mandatory = $True)]
[String]
$DomainName,
# Output root folder
[Parameter(Mandatory = $false)]
[String]
$Path = $PSScriptRoot,
# Include mailbox stats
[Parameter(Mandatory = $False)]
[switch]
$IncludeMailboxStats,
# Include inbox rules
[Parameter(Mandatory = $false)]
[switch]
$IncludeInboxRules,
# Include mailbox permissions
[Parameter(Mandatory = $false)]
[switch]
$IncludeMailboxPermissions,
# AD Credential
[Parameter(Mandatory = $false)]
[PSCredential]
$ADCredential,
# Skip AD User info
[Parameter(Mandatory = $False)]
[switch]
$SkipAdInfo
)
BEGIN {
$Global:ErrorActionPreference = 'Stop'
#############
# Functions #
#############
Function CreateFolder($Path) {
New-Item -Path $Path -ItemType Directory -Force | Out-Null
}
Function ExportXML($Object, $Path, $Email) {
$Email = $Email + ".xml"
$Path = Join-Path $Path $Email
$Object | Export-Clixml -Path $Path | Out-Null
}
Function TestRequiredModule() {
try {
Get-Command Get-Mailbox | Out-Null
}
Catch {
Write-Host "Not connected to Exchange Online. Please run Connect-ExchangeOnline before running this script." -ForegroundColor Red
Exit
}
Try {
Get-AzureADTenantDetail | Out-Null
}
Catch {
Write-Host "Not connected to Azure AD. Please run Connect-AzureAD before running this script." -ForegroundColor Red
Exit
}
Try {
Get-MsolAccountSku | Out-Null
}
Catch {
Write-Host "Not connected to MSOnline Service. Please run Connect-MsolService before running this script." -ForegroundColor Red
Exit
}
If (-not $SkipAdInfo) {
Try {
Get-Command Get-AdUser | Out-Null
}
Catch {
Write-Host "Missing AD PowerShell module. Install AD PowerShell module from RSAT before running this script." -ForegroundColor Red
Exit
}
}
}
Function ExportUserInfo($Email) {
$SourceEmailAddress = $Email
Write-Verbose "Getting user info..."
$User = Get-User $SourceEmailAddress
Write-Verbose "Exporting user info to XML.."
ExportXML -Object $User -Path $GetUserOutput -Email $SourceEmailAddress
Write-Verbose "Getting recipient info..."
$Recipient = Get-EXORecipient $SourceEmailAddress -PropertySets All
Write-Verbose "Exporting recipient info to XML..."
ExportXML -Object $Recipient -Path $GetRecipientOutput -Email $SourceEmailAddress
Write-Verbose "Getting mailbox info..."
$Mailbox = Get-EXOMailbox $SourceEmailAddress -PropertySets All
Write-Verbose "Exporting mailbox info to XML..."
ExportXML -Object $Mailbox -Path $GetMailboxOutput -Email $SourceEmailAddress
if ($mailbox.RecipientTypeDetails -eq "RoomMailbox" -or $mailbox.RecipientTypeDetails -eq "EquipmentMailbox") {
Write-Verbose "Getting calendaring processing info..."
$CalendarProcessing = Get-CalendarProcessing $SourceEmailAddress
Write-Verbose "Exporting calendar processing info to XML..."
ExportXML -Object $CalendarProcessing -Path $GetCalendarProcessing -Email $SourceEmailAddress
}
if ($IncludeMailboxStats) {
Write-Verbose "Getting mailbox statistics..."
$Statistics = Get-EXOMailboxStatistics $SourceEmailAddress -PropertySets All
Write-Verbose "Exporting mailbox statistics to XML..."
ExportXML -Object $Statistics -Path $GetMailboxStatisticsOutput -Email $SourceEmailAddress
}
if ($IncludeMailboxStats) {
if ($Mailbox.ArchiveGuid -ne '00000000-0000-0000-0000-000000000000') {
Write-Verbose "Getting archive mailbox statistics..."
$Archive = Get-EXOMailboxStatistics $SourceEmailAddress -PropertySets All -Archive
Write-Verbose "Exporting archive mailbox statistics to XML..."
ExportXML -Object $Archive -Path $GetMailboxStatisticsOutput -Email "Archive_$SourceEmailAddress"
}
else {
$Archive = $null
}
}
Write-Verbose "Getting Azure AD user info..."
$AzureADUser = Get-AzureADUser -ObjectId $Mailbox.UserPrincipalName
Write-Verbose "Exporting Azure AD user info to XML..."
ExportXML -Object $AzureADUser -Path $GetAzureADUserOutput -Email $SourceEmailAddress
Write-Verbose "Getting MSOL user info..."
$MSOLUser = Get-MsolUser -UserPrincipalName $Mailbox.UserPrincipalName
Write-Verbose "Exporting Msol user info to XML..."
ExportXML -Object $MSOLUser -Path $GetMsolUserOutput -Email $SourceEmailAddress
if ( -not $SkipAdInfo) {
if ($mailbox.IsDirSynced) {
Write-Verbose "Getting AD user info..."
$Escaped_UPN = $Mailbox.UserPrincipalName -replace "'", "''"
if ($ADCredential) {
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$($Escaped_UPN)'" -Server $DomainName -Properties * -Credential $ADCredential
}
else {
$ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$($Escaped_UPN)'" -Server $DomainName -Properties *
}
Write-Verbose "Exporting AD user info to XML..."
ExportXML -Object $ADUser -Path $GetADUserOutput -Email $SourceEmailAddress
}
else {
Write-Verbose "User is not synced from AD. Skipping AD user export..."
$ADUser = $null
}
}
if ($IncludeMailboxPermissions) {
Write-Verbose "Getting recipient permissions..."
$RecipientPermission = Get-EXORecipientPermission $SourceEmailAddress
Write-Verbose "Exporting recipient permissions to XML..."
ExportXML -Object $RecipientPermission -Path $GetRecipientPermissionOutput -Email $SourceEmailAddress
}
if ($IncludeMailboxPermissions) {
Write-Verbose "Getting mailbox folder permissions..."
$MailboxFolderPermission = Get-EXOMailboxFolderPermission $SourceEmailAddress
Write-Verbose "Exporting mailbox folder permissions to XML..."
ExportXML -Object $MailboxFolderPermission -Path $GetMailboxFolderPermissionOutput -Email $SourceEmailAddress
}
if ($IncludeMailboxStats) {
Write-Verbose "Getting mailbox folder statistics..."
$MailboxFolderStatistics = Get-EXOMailboxFolderStatistics $SourceEmailAddress
Write-Verbose "Exporting mailbox folder statistics to XML..."
ExportXML -Object $MailboxFolderStatistics -Path $GetMailboxFolderStatistics -Email $SourceEmailAddress
}
if ($IncludeMailboxStats) {
Write-Verbose "Getting mailbox sub folder permissions..."
$Folders = $MailboxFolderStatistics.folderpath | ForEach-Object { $_.replace("/", "\") }
$Permissions = $Folders | ForEach-Object { Get-EXOMailboxFolderPermission "$($SourceEmailAddress):$_" -ErrorAction silentlycontinue }
Write-Verbose "Exporting mailbox sub folder permissions to XML..."
ExportXML -Object $Permissions -Path $GetMailboxSubFolderPermissionOutput -Email $SourceEmailAddress
}
if ($IncludeInboxRules) {
Write-Verbose "Getting inbox rules..."
$Rules = Get-InboxRule -Mailbox $SourceEmailAddress
Write-Verbose "Exporting inbox rules to XML..."
ExportXML -Object $Rules -Path $GetInboxRulesOutput -Email $SourceEmailAddress
}
# CSV results
Return [PSCustomObject]@{
Alias = $Mailbox.Alias
FirstName = $User.FirstName
LastName = $User.LastName
DisplayName = $User.DisplayName
Name = $User.name
SamAccountName = $ADUser.SamAccountName
RecipientType = $User.RecipientType
RecipientTypeDetails = $User.RecipientTypeDetails
UserPrincipalName = $Mailbox.UserPrincipalName
PrimarySmtpAddress = $mailbox.PrimarySmtpAddress
ExchangeGuid = $Mailbox.ExchangeGuid
ArchiveGuid = $Mailbox.ArchiveGuid
LegacyExchangeDN = $Mailbox.LegacyExchangeDN
EmailAddresses = $Mailbox.EmailAddresses -join ";"
Manager = $User.Manager
Title = $User.Title
HomePhone = $User.HomePhone
MobilePhone = $User.MobilePhone
OtherHomePhone = $User.OtherHomePhone -join ";"
OtherTelephone = $User.OtherTelephone -join ";"
Phone = $User.Phone
Fax = $User.Fax
OtherFax = $User.OtherFax -join ";"
City = $User.City
Company = $User.Company
Department = $User.Department
Description = $ADUser.Description
Division = $ADUser.Division
EmployeeID = $ADUser.EmployeeID
EmployeeNumber = $ADUser.EmployeeNumber
extensionAttribute1 = $ADUser.extensionAttribute1
extensionAttribute2 = $ADUser.extensionAttribute2
extensionAttribute3 = $ADUser.extensionAttribute3
extensionAttribute4 = $ADUser.extensionAttribute4
extensionAttribute5 = $ADUser.extensionAttribute5
extensionAttribute6 = $ADUser.extensionAttribute6
extensionAttribute7 = $ADUser.extensionAttribute7
extensionAttribute8 = $ADUser.extensionAttribute8
extensionAttribute9 = $ADUser.extensionAttribute9
extensionAttribute10 = $ADUser.extensionAttribute10
extensionAttribute11 = $ADUser.extensionAttribute11
extensionAttribute12 = $ADUser.extensionAttribute12
extensionAttribute13 = $ADUser.extensionAttribute13
extensionAttribute14 = $ADUser.extensionAttribute14
extensionAttribute15 = $ADUser.extensionAttribute15
Office = $user.Office
Organization = $ADUser.Organization
PostalCode = $User.PostalCode
State = $User.StateOrProvince
StreetAddress = $User.StreetAddress
LitigationHoldEnabled = $Mailbox.LitigationHoldEnabled
LitigationHoldDate = $Mailbox.LitigationHoldDate
LitigationHoldOwner = $Mailbox.LitigationHoldOwner
LitigationHoldDuration = $Mailbox.LitigationHoldDuration
AcceptMessagesOnlyFrom = $Mailbox.AcceptMessagesOnlyFrom -join ";"
AcceptMessagesOnlyFromDLMembers = $Mailbox.AcceptMessagesOnlyFromDLMembers -join ";"
AcceptMessagesOnlyFromSendersOrMembers = $Mailbox.AcceptMessagesOnlyFromSendersOrMembers -join ";"
HiddenFromAddressListsEnabled = $Mailbox.HiddenFromAddressListsEnabled
MailboxSize = $Statistics.TotalItemSize
MailboxItemCount = $Statistics.ItemCount
ArchiveSize = $Archive.TotalItemSize
ArchiveItemCount = $Archive.ItemCount
}
}
Function ExportGroupInfo($Email) {
$SourceEmailAddress = $Email
Write-Verbose "Getting distribution group..."
$DL = Get-DistributionGroup $SourceEmailAddress
Write-Verbose "Exporting distribution group to XML..."
ExportXML -Object $DL -Path $GetDistributionGroupOutput -Email $SourceEmailAddress
Write-Verbose "Getting distribution group members..."
$DLMembers = Get-DistributionGroupMember $SourceEmailAddress
Write-Verbose "Exporting distribution group members to XML..."
ExportXML -Object $DLMembers -Path $GetDistributionGroupMemberOutput -Email $SourceEmailAddress
}
Function ExportUnifiedGroupInfo($Email) {
$SourceEmailAddress = $Email
Write-Verbose "Getting unified group..."
$UG = Get-UnifiedGroup $SourceEmailAddress
Write-Verbose "Exporting unified group to XML..."
ExportXML -Object $UG -Path $GetUnifiedGroupOutput -Email $SourceEmailAddress
Write-Verbose "Getting unified group members..."
$UGMembers = Get-UnifiedGroupLinks $SourceEmailAddress -LinkType Members
Write-Verbose "Exporting unified group members to XML..."
ExportXML -Object $UGMembers -Path $GetUnifiedGroupMemberOutput -Email $SourceEmailAddress
}
Function ExportDynamicGroupInfo($Email) {
$SourceEmailAddress = $Email
Write-Verbose "Getting dynamic group..."
$DDL = Get-DynamicDistributionGroup $SourceEmailAddress
Write-Verbose "Exporting dynamic group to XML..."
ExportXML -Object $DDL -Path $GetDynamicDistributionGroupOutput -Email $SourceEmailAddress
Write-Verbose "Getting dynamic group members..."
$DDLMembers = Get-DynamicDistributionGroupMember $SourceEmailAddress
Write-Verbose "Exporting dynamic group members to XML..."
ExportXML -Object $DDLMembers -Path $GetDynamicDistributionGroupMemberOutput -Email $SourceEmailAddress
}
Function ExportGuestUserInfo($Email) {
$SourceEmailAddress = $Email
Write-Verbose "Getting guest user info..."
$User = Get-User $SourceEmailAddress
Write-Verbose "Exporting guest user info to XML.."
ExportXML -Object $User -Path $GetGuestUserOutput -Email $SourceEmailAddress
Write-Verbose "Getting guest recipient info..."
$Recipient = Get-EXORecipient $SourceEmailAddress -PropertySets All
Write-Verbose "Exporting guest recipient info to XML..."
ExportXML -Object $Recipient -Path $GetGuestRecipientOutput -Email $SourceEmailAddress
Write-Verbose "Getting Azure AD guest user info..."
$AzureADUser = Get-AzureADUser -ObjectId $SourceEmailAddress
Write-Verbose "Exporting Azure AD guest user info to XML..."
ExportXML -Object $AzureADUser -Path $GetGuestAzureADUserOutput -Email $SourceEmailAddress
Write-Verbose "Getting MSOL guest user info..."
$MSOLUser = Get-MsolUser -UserPrincipalName $SourceEmailAddress
Write-Verbose "Exporting Msol user info to XML..."
ExportXML -Object $MSOLUser -Path $GetGuestMsolUserOutput -Email $SourceEmailAddress
Write-Verbose "Getting Azure AD guest user group membership..."
$AzureADGuestGroupMembership = Get-AzureADUserMembership -ObjectId $AzureADUser.ObjectId -All $true
Write-Verbose "Exporting Azure AD guest user group membership..."
ExportXML -Object $AzureADGuestGroupMembership -Path $GetGuestAzureADMembershipOutput -Email $SourceEmailAddress
}
##########
# Setup #
##########
# Test required commands
Write-Verbose "Testing required commands exist..."
TestRequiredModule
# Get Group Info
Try {
Write-Verbose "Getting distribution group $GroupName..."
$Group = Get-DistributionGroup -Identity $GroupName
}
Catch {
Write-Host "Group `"$GroupName`" does not exist. Please verify the group name and try again." -ForegroundColor Red
Break
}
Write-Verbose "Getting distribution group members for group $GroupName..."
$Members = Get-DistributionGroupMember -Identity $Group.Identity
if (-not $Members) {
Write-Host "Group `"$GroupName`" has no members. Please verify the group and try again."
Break
}
# Create Output Folders
$Date = Get-Date -Format yyyy-MM-dd_HH.mm
$Root = Join-Path $Path $Date
$GetUserOutput = Join-Path $Root "User_Output_XMLs"
$GetRecipientOutput = Join-Path $Root "Recipient_Output_XMLs"
$GetMailboxOutput = Join-Path $Root "Mailbox_Output_XMLs"
$GetCalendarProcessing = Join-Path $Root "Calendar_Processing_XMLs"
$GetMailboxStatisticsOutput = Join-Path $Root "Mailbox_Statistics_XMLs"
$GetAzureADUserOutput = Join-Path $Root "Azure_AD_User_Output_XMLs"
$GetMsolUserOutput = Join-Path $Root "MSOL_User_Output_XMls"
$GetADUserOutput = Join-Path $Root "AD_User_Output_XMLs"
$GetRecipientPermissionOutput = Join-Path $Root "Recipient_Permission_XMLs"
$GetMailboxFolderPermissionOutput = Join-Path $Root "Mailbox_Folder_Permission_XMLs"
$GetMailboxFolderStatistics = Join-Path $Root "Mailbox_Folder_Statistics_XMLs"
$GetMailboxSubFolderPermissionOutput = Join-Path $Root "Mailbox_Sub_Folder_Permission_XMLs"
$GetInboxRulesOutput = Join-Path $Root "Inbox_Rules_XMLs"
$GetDistributionGroupOutput = Join-Path $Root "Distribution_Group_XMLs"
$GetDistributionGroupMemberOutput = Join-Path $Root "Distribution_Group_Member_XMLs"
$GetUnifiedGroupOutput = Join-Path $Root "Unified_Group_XMLs"
$GetUnifiedGroupMemberOutput = Join-Path $Root "Unified_Group_Member_XMLs"
$GetDynamicDistributionGroupOutput = Join-Path $Root "Dynamic_Distribution_Group_XMLs"
$GetDynamicDistributionGroupMemberOutput = Join-Path $Root "Dynamic_Distribution_Group_Member_XMLs"
$GetGuestUserOutput = Join-Path $Root "Guest_User_Output_XMLs"
$GetGuestRecipientOutput = Join-Path $Root "Guest_Recipient_Output_XMLs"
$GetGuestAzureADUserOutput = Join-Path $Root "Guest_Azure_AD_User_Output_XMLs"
$GetGuestMsolUserOutput = Join-Path $Root "Guest_MSOL_User_Output_XMls"
$GetGuestAzureADMembershipOutput = Join-Path $Root "Guest_Azure_AD_User_Group_Membership_Output_XMLs"
$CsvExport = Join-Path $Root "CSV_Exports"
$Folders = @(
$GetUserOutput,
$GetRecipientOutput,
$GetMailboxOutput,
$GetCalendarProcessing,
$GetMailboxStatisticsOutput,
$GetAzureADUserOutput,
$GetMsolUserOutput,
$GetADUserOutput,
$GetRecipientPermissionOutput,
$GetMailboxFolderPermissionOutput,
$GetMailboxFolderStatistics,
$GetMailboxSubFolderPermissionOutput,
$GetInboxRulesOutput,
$GetDistributionGroupOutput,
$GetDistributionGroupMemberOutput,
$GetUnifiedGroupOutput,
$GetUnifiedGroupMemberOutput,
$GetDynamicDistributionGroupOutput,
$GetDynamicDistributionGroupMemberOutput,
$GetGuestUserOutput,
$GetGuestRecipientOutput,
$GetGuestAzureADUserOutput,
$GetGuestMsolUserOutput,
$GetGuestAzureADMembershipOutput,
$CsvExport
)
foreach ($Path in $Folders) {
Write-Verbose "Creating output folder $Path..."
CreateFolder -Path $Path
}
}
PROCESS {
$Total = $Members | Measure-Object | Select-Object -ExpandProperty Count
$i = 0
$CSV_Results = @()
$ErrorList = @()
foreach ($Member in $Members) {
Write-Progress -Id 1 -Activity "Exporting info..." -Status "Objects: [$i/$Total] | Errors: $($ErrorList.Length)" -PercentComplete ($i / $Total * 100) -CurrentOperation "Working on object $($Member.PrimarySmtpAddress)"
$i++
$SourceEmailAddress = $Member.PrimarySmtpAddress
# If user object
if ($Member.RecipientType -eq "UserMailbox") {
# Get user info
Write-Verbose "Working on user $SourceEmailAddress..."
try {
$CSV_Results += ExportUserInfo -Email $SourceEmailAddress
}
Catch {
$err = $_
Write-Verbose "Failed to export user $SourceEmailAddress"
$ErrObject = [PSCustomObject]@{
"Email" = $SourceEmailAddress
"RecipientType" = $Member.RecipientType
"RecipientTypeDetails" = $Member.RecipientTypeDetails
"Error" = $err
}
$ErrorList += $ErrObject
}
}
# If group object
elseif ($Member.RecipientTypeDetails -eq "MailUniversalDistributionGroup" -or $Member.RecipientTypeDetails -eq "MailUniversalSecurityGroup") {
# Get group info
Write-Verbose "Working on group $SourceEmailAddress..."
try {
ExportGroupInfo -Email $SourceEmailAddress
}
Catch {
$err = $_
Write-Verbose "Failed to export group $SourceEmailAddress"
$ErrObject = [PSCustomObject]@{
"Email" = $SourceEmailAddress
"RecipientType" = $Member.RecipientType
"RecipientTypeDetails" = $Member.RecipientTypeDetails
"Error" = $err
}
$ErrorList += $ErrObject
}
}
# unified groups (O365 Groups Teams)
elseif ($Member.RecipientTypeDetails -eq "GroupMailbox") {
Write-Verbose "Working on unified group $SourceEmailAddress..."
try {
ExportUnifiedGroupInfo -Email $SourceEmailAddress
}
Catch {
$err = $_
Write-Verbose "Failed to export unified group $SourceEmailAddress"
$ErrObject = [PSCustomObject]@{
"Email" = $SourceEmailAddress
"RecipientType" = $Member.RecipientType
"RecipientTypeDetails" = $Member.RecipientTypeDetails
"Error" = $err
}
$ErrorList += $ErrObject
}
}
# dynamic distribution groups
elseif ($Member.RecipientTypeDetails -eq "DynamicDistributionGroup") {
Write-Verbose "Working on dynamic group $SourceEmailAddress..."
try {
ExportDynamicGroupInfo -Email $SourceEmailAddress
}
Catch {
$err = $_
Write-Verbose "Failed to export dynamic group $SourceEmailAddress"
$ErrObject = [PSCustomObject]@{
"Email" = $SourceEmailAddress
"RecipientType" = $Member.RecipientType
"RecipientTypeDetails" = $Member.RecipientTypeDetails
"Error" = $err
}
$ErrorList += $ErrObject
}
}
# Guest users (GuestMailUser)
elseif ($Member.RecipientTypeDetails -eq "GuestMailUser") {
$SourceEmailAddress = $Member.WindowsLiveID
Write-Verbose "Working on guest user $SourceEmailAddress..."
try {
ExportGuestUserInfo -Email $SourceEmailAddress
}
Catch {
$err = $_
Write-Verbose "Failed to export guest user $SourceEmailAddress"
$ErrObject = [PSCustomObject]@{
"Email" = $SourceEmailAddress
"RecipientType" = $Member.RecipientType
"RecipientTypeDetails" = $Member.RecipientTypeDetails
"Error" = $err
}
$ErrorList += $ErrObject
}
}
# unknown object
else {
$ErrObject = [PSCustomObject]@{
"Name" = $Member.Name
"RecipientType" = $Member.RecipientType
"RecipientTypeDetails" = $Member.RecipientTypeDetails
"Error" = "Not parsed by script"
}
$ErrorList += $ErrObject
}
}
#Export CSV results
if ($CSV_Results) {
$CSV_Results | Export-Csv -NoTypeInformation (Join-Path $CsvExport "Export.csv")
}
if ($ErrorList) {
$ErrorList | Export-Csv -NoTypeInformation (Join-Path $CsvExport "Error_Export.csv")
}
}
END {
Write-Host "Results saved to: $Root"
}