Copy-StaticGAL.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
<#PSScriptInfo
 
.VERSION 1.3
 
.GUID 941ee5e7-c753-4bd5-acea-e2d73a37aa9e
 
.DESCRIPTION Create a static GAL of another organization's users for organizations that can't deploy FIM/MIM.
 
.AUTHOR Aaron Guilmette
 
.COMPANYNAME Microsoft
 
.COPYRIGHT 2022
 
.TAGS Global Address List, GAL, GALSync
 
.LICENSEURI
 
.PROJECTURI https://www.undocumented-features.com/2016/10/19/synchronizing-objects-between-tenants/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>


<#
.SYNOPSIS
Global Address List Export/Import.
 
This script will capture all recipients in a source tenant and recreate them
as contacts in a target tenant, excluding objects (by default) whose primary
SMTP address matches an accepted domain in the target tenant. This script is
useful in a divestiture scenario (where one environment is splitting into one or
more separate environments) and there is a need to maintain replyability for
objects that are cached in Outlook profiles.
 
If you have the ability to configure Forefront Identity Manager or AADConnect to
sync objects, it is preferred, as this is more of a "point in time" copy and
does not support delta updates or deletions in the target environment, since it
is not using the a sync API. While it doesn't support deltas, it will allow
you to run it multiple times and overwrite existing objects in the target
environment.
 
.PARAMETER ConfirmDeletes
Confirm deletes in target environment if ProcessDeletes switch is used.
 
.PARAMETER ExcludedDomains
List of primary SMTP domains. Objects whose PrimarySmtpAddress value matches
any domain in this list will NOT be imported as a contact.
 
.PARAMETER ExcludedObjects
List of source objects (as email address) to exclude. By default, the
DiscoverySearch mailbox is excluded.
 
.PARAMETER ExportFile
Specify a custom export file to save the GAL objects.
 
.PARAMETER ExportOnly
Create only an export file from the source environment.
 
.PARAMETER InputFile
Specify an input file to import a previously exported GALSync.
 
.PARAMETER ProcessTargetDeletes
Delete contact objects synced to target envrionment that do not appear in source
environment. Must include ConfirmDeletes switch to actually process deletes.
 
.PARAMETER RecipientTypes
Specify types of recipients to include in export/import.
 
.PARAMETER ResultSize
Use this to export a fixed number of objects. Useful for testing the
process on a small scale. Defaults to "Unlimited" if no value is specified. This
parameter has no effect on imports.
 
.PARAMETER SourceConnectionUri
Set the source connection URI.
 
.PARAMETER SourceCredential
Source environment credentials.
 
.PARAMETER SourceEnvironment
Determines authentication scheme used. Select ExchangeOnline (default) for Basic
or ExchangeOnPrem for Kerberos.
 
.PARAMETER SyncAttribute
Use this parameter to specify a custom attribute in which to store metadata or
information about the synced object. For example, you may want to use this
attribute to flag objects that were imported.
 
.PARAMETER SyncAttributeValue
Specify a value to place in the SyncAttribute.
 
.PARAMETER TargetConnectionUri
Set the target connection URI.
 
.PARAMETER TargetCredential
Target environment credential.
 
.PARAMETER TargetEnvironment
Determines authentication scheme used. Select ExchangeOnline (default) for Basic
or ExchangeOnPrem for Kerberos.
 
.EXAMPLE
.\Copy-StaticGAL.ps1 -RecipientTypes UserMailbox,MailUser -ResultSize 2
 
Copy 2 UserMailbox and MailUser objects from source tenant to target tenant as
contact objects and prompt for source/target credentials.
 
.EXAMPLE
.\Copy-StaticGAL.ps1 -RecipientTypes UserMailbox -ResultSize 2 -ExcludedObjects user1@fabrikam.com,user2@contoso.com
 
Copy 2 UserMailbox objects from source tenant to target tenant contact objects,
except user1@fabrikam.com and user2@contoso.com.
 
.EXAMPLE
.\Copy-StaticGAL.ps1 -InputFile GALExport.Csv -TargetConnectionUri 'https://ExchangeServer1/PowerShell/' TargetEnvironment ExchangeOnPrem
 
Import the file GALExport.csv as contacts into the target on-premises environment
via ExchangeServer1 with Kerberos authentication.
 
.EXAMPLE
.\Copy-StaticGAL.ps1 -ExportOnly
 
Export a copy of the GAL to CSV from an Office 365 tenant and prompt for
credentials.
 
.EXAMPLE
.\Copy-StaticGAL.ps1 -InputFile GALExport.csv -RecipientTypes MailUser -TargetCredential $TargetCred -SyncAttribute CustomAttribute15 -SyncAttributeValue StaticGAL
 
Import MailUser objects from file GALExport.csv into target tenant using stored
PSCredential object $TargetCred. Set CustomAttribute15 on target contact object
to 'StaticGAL'.
 
.EXAMPLE
.\Copy-StaticGAL.ps1 -InputFile GALExport.csv -TargetCredential $TargetCred -SyncAttribute CustomAttribute15 -SyncAttributeValue StaticGAL -ProcessTargetDeletes -ConfirmDeletes
 
Import objects from file GALExport.csv into target tenant using stored
PSCredential object $TargetCred. Set CustomAttribute15 on target contact object
to 'StaticGAL'. Delete objects in target environment with CustomAttribute15 set
to 'StaticGAL' that are not present in the GALExport.csv input file.
 
.LINK
 
.NOTES
$SourceCredential and $TargetCredential take standard PSCredential objects.
 
It would be best to test by setting -ResultSize to a low integer and running
several different RecipentTyes (default is all types) to ensure that the process
is working correctly between your tenants and that the target objects look as
anticipated.
 
Revision List:
2022-01-24 : Published to PS Gallery.
2016-07-23 : Added delete functionality
2016-07-22 : Updated ExcludedDomains syntax to always exclude accepted
                domains in target environment.
                 
                Added -ExportOnly switch.
#>

param(
    [switch]$ConfirmDeletes,
    [array]$ExcludedDomains,
    [array]$ExcludedObjects,
    [string]$ExportFile = "GALSyncExport.csv",
    [switch]$ExportOnly,
    [string]$InputFile,
    [switch]$ProcessTargetDeletes,
    [ValidateSet('UserMailbox','MailUser','MailContact','MailUniversalSecurityGroup','MailUniversalDistributionGroup')]
        [array]$RecipientTypes = ('UserMailbox','MailUser','MailContact','MailUniversalSecurityGroup','MailUniversalDistributionGroup'),
    [ValidatePattern("^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9]|Unlimited)$")]
        [string]$ResultSize = "Unlimited",
    [string]$SourceConnectionUri = "https://outlook.office365.com/powershell-liveid/",
    [System.Management.Automation.PSCredential]$SourceCredential,
    [ValidateSet('ExchangeOnline','ExchangeOnPrem')]
        [string]$SourceEnvironment = "ExchangeOnline",
    [ValidateSet('CustomAttribute1','CustomAttribute2','CustomAttribute3','CustomAttribute4','CustomAttribute5','CustomAttribute6','CustomAttribute7','CustomAttribute8','CustomAttribute9','CustomAttribute10','CustomAttribute11','CustomAttribute12','CustomAttribute13','CustomAttribute14','CustomAttribute15')]
        [string]$SyncAttribute,
    [string]$SyncAttributeValue = "StaticGAL",
    [string]$TargetConnectionUri = "https://outlook.office365.com/powershell-liveid/",
    [System.Management.Automation.PSCredential]$TargetCredential,
    [ValidateSet('ExchangeOnline','ExchangeOnPrem')]
        [string]$TargetEnvironment = "ExchangeOnline"
    )

Function SourceEnvironmentLogon
    {
    If (!$SourceCredential) { $SourceCredential = Get-Credential -Message "Souce Credential" }
    If ($SourceEnvironment -eq "ExchangeOnline") { Write-Host "Using Basic Authentication."; $Authentication = "Basic" }
    If ($SourceEnvironment -eq "ExchangeOnPrem") { Write-Host "Using Kerberos Authentication."; $Authentication = "Kerberos" }
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $SourceConnectionUri -Credential $SourceCredential -Authentication $Authentication -AllowRedirection -ErrorAction Stop -WarningAction SilentlyContinue
    Import-PSSession $Session -ErrorAction Stop -WarningAction SilentlyContinue -DisableNameChecking | Out-Null
    }
    
Function TargetEnvironmentLogon
    {
    If (!$TargetCredential) {$TargetCredential = Get-Credential -Message "Target Credential"}
    If ($SourceEnvironment -eq "ExchangeOnline") { Write-Host "Using Basic Authentication."; $Authentication = "Basic" }
    If ($SourceEnvironment -eq "ExchangeOnPrem") { Write-Host "Using Kerberos Authentication."; $Authentication = "Kerberos" }
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $TargetConnectionUri -Credential $TargetCredential -Authentication $Authentication -AllowRedirection -ErrorAction Stop -WarningAction SilentlyContinue
    Import-PSSession $Session -ErrorAction Stop -WarningAction SilentlyContinue -DisableNameChecking | Out-Null
    }

$StartDate = Get-Date
Write-Host "Started at $($StartDate)."

If (!$InputFile)
    {
    Write-Host "Connecting to Source Environment - $($SourceConnectionUri)."
    SourceEnvironmentLogon
    Write-Host "Building Recipient List. This may take a long time." 
    [array]$SourceGAL = @()
    Foreach ($Type in $RecipientTypes)
        {
        Switch ($Type)
            {
            UserMailbox
                {
                Write-Host "Exporting $($Resultsize) UserMailbox objects."
                $SourceGAL += Get-Mailbox -Resultsize $ResultSize -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
                }
            MailUser
                {
                Write-Host "Exporting $($Resultsize) MailUser objects."
                $SourceGAL += Get-MailUser -Resultsize $ResultSize -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
                }
            MailContact
                {
                Write-Host "Exporting $($Resultsize) MailContact objects."
                $SourceGAL += Get-MailContact -ResultSize $ResultSize -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
                }
            MailUniversalSecurityGroup
                {
                Write-Host "Exporting $($Resultsize) MailUniversalSecurityGroup objects."
                $SourceGAL += Get-DistributionGroup -RecipientTypeDetails MailUniversalSecurityGroup -ResultSize $ResultSize -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
                }
            MailUniversalDistributionGroup
                {
                Write-Host "Exporting $($Resultsize) MailUniversalDistributionGroup objects."
                $SourceGAL += Get-DistributionGroup -RecipientTypeDetails MailUniversalDistributionGroup -ResultSize $ResultSize -WarningAction SilentlyContinue -ErrorAction SilentlyContinue
                }
            } # End Switch
        } # End Foreach $Type
    
    Write-Host "Disconnecting from Source Environment."
    Get-PSSession | Remove-PSSession

    # Export CSVs for later review
    Write-Host "Exporting GAL objects to CSV $($ExportFile)."
    $SourceGAL | Select Alias,DisplayName,@{n="EmailAddresses";e={$_.EmailAddresses -join ";"}},ExternalEmailAddress,FirstName,HiddenFromAddressListsEnabled,LastName,LegacyExchangeDn,Name,PrimarySmtpAddress,RecipientType | Export-Csv -NoTypeInformation $ExportFile -Force 

    # If ExportOnly mode, exit.
    If ($ExportOnly)
        {
        Break
        } # End IF $ExportOnly
    } # End If $InputFile

Write-Host "Connecting to Target Environment - $($TargetConnectionUri)."
TargetEnvironmentLogon

# Build the domain-filtered list of objects to add to the GAL
If (!$ExcludedDomains)
    {
    [System.Collections.ArrayList]$DomainFilter = (Get-AcceptedDomain).DomainName | ? { $_ -notlike "*onmicrosoft.com" }
    }
Else
    {
    [System.Collections.ArrayList]$DomainFilter = (Get-AcceptedDomain).DomainName | ? { $_ -notlike "*onmicrosoft.com" }
    $DomainFilter += $ExcludedDomains
    }

Write-Host "Filtering out objects in the following domains:"
Write-Host -ForegroundColor Green $DomainFilter

Write-Host "Importing filtered object list."

# Build exclude list. By default, only includes the DiscoverySearchMailbox.
If (!$ExcludedObjects)
    {
    [array]$ExcludedObjects = (Get-Mailbox -anr DiscoverySearchMailbox).PrimarySmtpAddress
    } # End If !$ExcludedObjects

Write-Host -NoNewline "Exclude list contains ";Write-Host -NoNewline $ExcludedObjects.Count;Write-Host " objects."    

[int]$i = 1
[int]$MailContacts = 0
[int]$Mailboxes = 0
[int]$Mailusers = 0
[int]$DistributionGroups = 0
[int]$SecurityGroups = 0

If ($InputFile)
    {
    # If $RecipientTypes is specified in for Input file, filter the RecipientTypes
    If ($RecipientTypes)
        {
        $RecipientTypeFilter = ‘(?i)^(‘ + (($RecipientTypes |foreach {[regex]::escape($_)}) -join "|") + ‘)$’
        Write-Host "Applying RecipientTypes filter $($RecipientTypeFilter) to import list."
        $SourceGAL = Import-Csv $InputFile | ? { $_.RecipientType -match $RecipientTypeFilter }
        } # End If $RecipientTypes
    Else
        {
        $SourceGAL = Import-Csv $InputFile
        } # End If/Else $RecipientTypes
    Write-Host "Processing from file import."
    $objCount = $SourceGAL.Count
    Write-Host "$($objCount) total objects will be processed."
    Sleep 10
    Foreach ($GALObj in $SourceGAL)
        {
        # Filter out objects whose right-hand side (after @) are contained in the $DomainFilter Array or any object in the $ExcludedObjects array
        If ($DomainFilter -notcontains $GALObj.PrimarySmtpAddress.Split("@")[1] -or $ExcludedObjects -notcontains $GALObj.PrimarySmtpAddress)
            {
            $RecipientType = $GALObj.RecipientType
            Switch ($RecipientType)
                {
                UserMailbox
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $ExternalEmailAddress = $GALObj.PrimarySmtpAddress
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses.Split(";") + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $FirstName = $GALObj.FirstName
                    $LastName = $GALObj.LastName
                    $Name = $GALObj.Name
                    # Needed to convert "True" or "False" value from text to Boolean
                    $HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($GALObj.HiddenFromAddressListsEnabled)
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -ForegroundColor DarkCyan "mailbox ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $Mailboxes++
                        }
                    } # End UserMailbox
              MailUser
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses.Split(";") + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.ExternalEmailAddress
                    $FirstName = $GALObj.FirstName
                    $LastName = $GALObj.LastName
                    $Name = $GALObj.Name
                    # Needed to convert "True" or "False" value from text to Boolean
                    $HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($GALObj.HiddenFromAddressListsEnabled)
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -Fore DarkGreen "mailuser ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $Mailusers++
                        }
                    } # End MailUser
              MailContact
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses.Split(";") + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $ExternalEmailAddress = $GALObj.ExternalEmailAddress
                    $EmailAddressesCount = $EmailAddresses.Count
                    $FirstName = $GALObj.FirstName
                    $LastName = $GALObj.LastName
                    $Name = $GALObj.Name
                    # Needed to convert "True" or "False" value from text to Boolean
                    $HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($GALObj.HiddenFromAddressListsEnabled)
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -ForegroundColor DarkMagenta "contact ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $MailContacts++
                        }        
                    } # End Contact
              MailUniversalSecurityGroup
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses.Split(";") + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.PrimarySmtpAddress
                    $Name = $GALObj.Name
                    # Needed to convert "True" or "False" value from text to Boolean
                    $HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($GALObj.HiddenFromAddressListsEnabled)
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -Fore DarkRed "Mail Universal Security Group "; Write-Host "for $($ExternalEmailAddress)."
                
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $SecurityGroups++
                        }
                    } # End MailUniversalSecurityGroup
              MailUniversalDistributionGroup
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses.Split(";") + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.PrimarySmtpAddress
                    $Name = $GALObj.Name
                    # Needed to convert "True" or "False" value from text to Boolean
                    $HiddenFromAddressListsEnabled = [System.Convert]::ToBoolean($GALObj.HiddenFromAddressListsEnabled)
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -Fore DarkYellow "Mail Universal Distribution Group ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $DistributionGroups++
                        }
                    } # End MailUniversalDistributionGroup
              } # End Switch
          } # End If
          $i++
          } # End ForEach
    } # End If $InputFile
Else
    {
    Write-Host "Processing from direct."
    $objCount = $SourceGAL.Count
    Write-Host "$($objCount) total objects will be processed."
    Sleep 10
    Foreach ($GALObj in $SourceGAL)
        {
        If ($DomainFilter -notcontains $GALObj.PrimarySmtpAddress.Split("@")[1] -or $ExcludedObjects -notcontains $GALObj.PrimarySmtpAddress)
            {
            $RecipientType = $GALObj.RecipientType
            Switch ($RecipientType)
                {
                UserMailbox
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $ExternalEmailAddress = $GALObj.PrimarySmtpAddress
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $FirstName = $GALObj.FirstName
                    $LastName = $GALObj.LastName
                    $Name = $GALObj.Name
                    $HiddenFromAddressListsEnabled = $GALObj.HiddenFromAddressListsEnabled
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -ForegroundColor DarkCyan "mailbox ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $Mailboxes++
                        }
                    } # End UserMailbox
              MailUser
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.ExternalEmailAddress
                    $FirstName = $GALObj.FirstName
                    $LastName = $GALObj.LastName
                    $Name = $GALObj.Name
                    $HiddenFromAddressListsEnabled = $GALObj.HiddenFromAddressListsEnabled
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -Fore DarkGreen "mailuser ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $Mailusers++
                        }
                    } # End MailUser
              MailContact
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.ExternalEmailAddress
                    $FirstName = $GALObj.FirstName
                    $LastName = $GALObj.LastName
                    $Name = $GALObj.Name
                    $HiddenFromAddressListsEnabled = $GALObj.HiddenFromAddressListsEnabled
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -ForegroundColor DarkMagenta "contact ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $MailContacts++
                        }        
                    } # End Contact
              MailUniversalSecurityGroup
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.PrimarySmtpAddress
                    $Name = $GALObj.Name
                    $HiddenFromAddressListsEnabled = $GALObj.HiddenFromAddressListsEnabled
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -Fore DarkRed "Mail Universal Security Group "; Write-Host "for $($ExternalEmailAddress)."
                
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $SecurityGroups++
                        }
                    } # End MailUniversalSecurityGroup
              MailUniversalDistributionGroup
                    {
                    $Alias = $GALObj.Alias
                    $DisplayName = $GALObj.DisplayName
                    $LegDN = "x500:"+$GALObj.LegacyExchangeDN
                    [array]$EmailAddresses = $GALObj.EmailAddresses + $LegDN
                    [array]$EmailAddresses = $EmailAddresses | Sort -Unique
                    $EmailAddressesCount = $EmailAddresses.Count
                    $ExternalEmailAddress = $GALObj.PrimarySmtpAddress
                    $Name = $GALObj.Name
                    $HiddenFromAddressListsEnabled = $GALObj.HiddenFromAddressListsEnabled
                    Write-Host "[$i/$objCount]"
                    Write-Host -NoNewline "Source object is a ";Write-Host -NoNewLine -Fore DarkYellow "Mail Universal Distribution Group ";Write-Host "for $($ExternalEmailAddress)."
                    
                    # Provision the object
                    Try
                        {
                        Write-Host -NoNewline "Trying to create new contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        New-MailContact -Alias $Alias -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName -Name $Name -ea SilentlyContinue
                        Write-Host " Updating $($EmailAddressesCount) proxy addresses..."
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Catch
                        {
                        Write-Host -NoNewline "Trying to update contact for ";Write-Host -ForegroundColor Green $ExternalEmailAddress
                        Set-MailContact $Alias -HiddenFromAddressListsEnabled $HiddenFromAddressListsEnabled -EmailAddresses $EmailAddresses -Name $Name -ea SilentlyContinue
                        If ($SyncAttribute)
                            {
                            $cmd = "Set-MailContact $Alias -$SyncAttribute $SyncAttributeValue"
                            Invoke-Expression $cmd
                            }
                        Write-Host "-----`n"
                        }
                    Finally
                        {
                        $DistributionGroups++
                        }
                    } # End MailUniversalDistributionGroup
              } # End Switch
          } # End If
          # Increment $i for counter
        $i++
          } # End ForEach
    } # End Else

If ($ProcessTargetDeletes)
    {
    # Build source environment hash table based on Alias
    $SourceHash = @{}
    $TargetHash = @{}
        
    Write-Host "Step [1/4] - Building source object list for processing target environment deletes."
    Foreach ($GALObj in $SourceGAL)
        {
        If ($DomainFilter -notcontains $GALObj.PrimarySmtpAddress.Split("@")[1] -or $ExcludedObjects -notcontains $GALObj.PrimarySmtpAddress)
            {
            $SourceHash[$GALObj.Alias]=$GALObj.PrimarySmtpAddress
            }
        }
        
    # Build target environment hash table based on Alias and Get-Recipient
    Write-Host "Step [2/4] - Building target environment list for processing deleted items. This may take a while."
    If ($SyncAttribute)
        {
        Get-MailContact -ResultSize Unlimited | ? { $_.$($SyncAttribute) -eq $($SyncAttributeValue) }
        }
    Else
        {
        $TargetGAL = Get-MailContact -Resultsize Unlimited
        }

    $TargetHash[$TargetGAL.Alias]=$TargetGAL.ExternalEmailAddress
    $TargetHashCount = $TargetHash.Count
    Write-Host "Imported $($TargetHashCount) objects from target environment."
    
    # Perform the hash compare
    Write-Host "Step [3/4] - Preparing list of objects to delete. This may take a while."
    $Deletes = $TargetHash.Keys | ? { $_ -notin $SourceHash.Keys }
    $DeletesCount = $Deletes.Count
    
    # Delete target environment contacts
    $i = 0
    Write-Host "Step [4/4] - Processing deletes."
    $DeleteCmd = "Remove-MailContact $obj -confirm:$false -whatif | out-null"
    If ($ConfirmDeletes)
        {
        $DeleteCmd = "Remove-MailContact $obj -confirm:$false | out-null"
        }
    Foreach ($obj in $Deletes)
        {
        Write-Host "Processing [$i/$DeletesCount] - $($obj) for deletion."
        Invoke-Expression $DeleteCmd
        $i++
        }        
    } # End If ($ProcessTargetDeletes)
    
Write-Host "Disconnecting from Target Environment."
Get-PSSession | Remove-PSSession
$EndDate = Get-Date
Write-host "Ended at $($EndDate)."
Write-Host "Elapsed time:"
$EndDate - $StartDate
Write-Host "Total number of items processed:"
Write-Host "UserMailboxes: $($Mailboxes)"
Write-Host "MailUsers: $($MailUsers)"
Write-Host "MailContacts: $($MailContacts)"
Write-Host "Distribution Groups: $($DistributionGroups)"
Write-Host "Security Groups: $($SecurityGroups)"