-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathset-Office365GroupMV.ps1
1363 lines (1067 loc) · 62.5 KB
/
set-Office365GroupMV.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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
This function sets the multi valued attributes of the DL
.DESCRIPTION
This function sets the multi valued attributes of the DL.
For each of use - I've combined these into a single function instead of splitting them out.
.OUTPUTS
None
.EXAMPLE
set-Office365DLMV -originalDLConfiguration -exchangeDLMembership -exchangeRejectMessage -exchangeAcceptMessage -exchangeManagedBy -exchangeModeratedBy -exchangeBypassMOderation -exchangeGrantSendOnBehalfTo.
#>
Function set-Office365GroupMV
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
$originalDLConfiguration,
[Parameter(Mandatory = $true)]
$office365DLConfiguration,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeDLMembershipSMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeRejectMessagesSMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeAcceptMessageSMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeManagedBySMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeModeratedBySMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeBypassModerationSMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeGrantSendOnBehalfToSMTP=$NULL,
[Parameter(Mandatory = $true)]
[AllowEmptyCollection()]
[array]$exchangeSendAsSMTP=$NULL,
[Parameter(Mandatory=$true)]
$office365DLConfigurationPostMigration,
[Parameter(Mandatory=$TRUE)]
$mailOnMicrosoftComDomain,
[Parameter(Mandatory=$TRUE)]
$allowNonSyncedGroup=$FALSE,
[Parameter(Mandatory=$TRUE)]
$allOffice365SendAsAccessOnGroup=$NULL,
[Parameter(Mandatory=$FALSE)]
[boolean]$isFirstAttempt=$false,
[Parameter(Mandatory=$true)]
$exchangeOnlineCredential
)
#Output all parameters bound or unbound and their associated values.
write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore)
#Declare function variables.
[array]$functionDirectoryObjectID = $NULL
$functionEmailAddress = $NULL
[boolean]$routingAddressIsPresent=$FALSE
[string]$hybridRemoteRoutingAddress=$NULL
[int]$functionLoopCounter=0
[boolean]$functionFirstRun=$TRUE
[array]$functionRecipients=@()
[array]$functionEmailAddresses=@()
[string]$functionMail=""
[string]$functionMailNickname=""
[string]$functionExternalDirectoryObjectID = ""
[string]$functionEmailAddressToRemove = ""
[boolean]$isTestError=$false
[array]$functionErrors=@()
#Start function processing.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "BEGIN set-Office365GroupMV"
Out-LogFile -string "********************************************************************************"
if ($office365DLConfigurationPostMigration.externalDirectoryObjectID -eq "")
{
$functionExternalDirectoryObjectID = $office365DLConfigurationPostMigration.GUID
}
else
{
$functionExternalDirectoryObjectID = $office365DLConfigurationPostMigration.externalDirectoryObjectID
}
out-logfile -string "External directory object ID utilized for set commands:"
out-logfile -string $functionExternalDirectoryObjectID
if ($originalDLConfiguration.mailNickName -ne $NULL)
{
out-logfile -string "Mail nickname present on premises -> using this value."
$functionMailNickName = $originalDLConfiguration.mailNickName
out-logfile -string $functionMailNickName
}
else
{
out-logfile -string "Mail nickname not present on premises -> using Office 365 value."
$functionMailNickName = $office365DLConfiguration.alias
out-logfile -string $functionMailNickName
}
out-logfile -string "Determine if this is a first pass operation."
if ($isFirstAttempt -eq $FALSE)
{
out-logfile -string "This is not the first pass - update items that would conflict with existing group."
out-logfile -string "Resetting all SMTP addresses on the object to match on premises."
out-logfile -string "Resetting all SMTP addresses on the object to match on premises."
foreach ($address in $originalDLConfiguration.proxyAddresses)
{
if (($address.tolower()).contains($mailOnMicrosoftComDomain.tolower()))
{
out-logfile -string ("Hybrid remote routing address found.")
out-logfile -string $address
$routingAddressIsPresent=$TRUE
}
out-logfile -string $address
$functionEmailAddresses+=$address.tostring()
}
foreach ($address in $office365DLConfiguration.emailAddresses)
{
if (($address.tolower()).contains($mailOnMicrosoftComDomain.tolower()))
{
out-logfile -string ("Hybrid remote routing address found.")
out-logfile -string $address
$routingAddressIsPresent=$TRUE
}
out-logfile -string $address
$functionEmailAddresses+=$address.tostring()
}
$functionEmailAddresses = $functionEmailAddresses | select-object -unique
out-logfile -string $functionEmailAddresses
$functionEmailAddressToRemove = $office365DLConfigurationPostMigration.primarySMTPAddress
out-logfile -string "Email address to remove after resetting attributes."
out-logfile -string $functionEmailAddressToRemove
#With the new temp DL logic - the fast deletion and then immediately moving into set operations sometimes caused cache collisions.
#This caused the following bulk logic to fail - then the individual set logics would also fail.
#This left us with the temp DL without any actual SMTP addresses.
#New logic - try / sleep 10 times then try the individuals.
$maxRetries = 0
Do
{
try {
$isTestError=$FALSE
out-logfile -string ("Max retry attempt: "+$maxRetries.toString())
Set-O365UnifiedGroup -identity $functionExternalDirectoryObjectID -emailAddresses $functionEmailAddresses -errorAction STOP
$maxRetries = 10 #The previous set was successful - so immediately bail.
}
catch {
out-logfile -string "Error bulk updating email addresses on distribution group."
out-logfile -string $_
$isTestError=$TRUE
out-logfile -string "Starting 10 second sleep before trying bulk update."
start-sleep -s 10
$maxRetries = $maxRetries+1
}
}
while($maxRetries -lt 10)
if ($isTestError -eq $TRUE)
{
out-logfile -string "Attempting SMTP address updates per address."
out-logfile -string "Establishing group primary SMTP Address."
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -primarySMTPAddress $originalDLConfiguration.mail -errorAction STOP
}
catch {
out-logfile -string "Error establishing new group primary SMTP Address."
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Proxy Addresses"
ErrorMessage = ("Unable to set cloud distribution group primary SMTP address to match on-premises mail address.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
foreach ($address in $functionEmailAddresses)
{
out-logfile -string ("Processing address: "+$address)
try{
Set-O365UnifiedGroup -identity $functionExternalDirectoryObjectID -emailAddresses @{add=$address} -errorAction STOP
}
catch{
out-logfile -string ("Error processing address: "+$address)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Proxy Addresses"
ErrorMessage = ("Address "+$address+" could not be added to new cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
}
#Operation set complete - reset isError.
$isTestError=$FALSE
if ($originalDLConfiguration.legacyExchangeDN -ne $NULL)
{
out-logfile -string "Processing on premises legacy ExchangeDN to X500"
out-logfile -string $originalDLConfiguration.legacyExchangeDN
$functionEmailAddress = "X500:"+$originalDLConfiguration.legacyExchangeDN
out-logfile -string ("The x500 address to process = "+$functionEmailAddress)
try {
Set-O365UnifiedGroup -identity $functionExternalDirectoryObjectID -emailAddresses @{add=$functionEmailAddress} -errorAction STOP
}
catch {
out-logfile -string ("Error processing address: "+$functionEmailAddress)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Proxy Addresses"
ErrorMessage = ("Address "+$functionEmailAddress+" could not be added to new cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
if ($allowNonSyncedGroup -eq $FALSE)
{
out-logfile -string "Processing original cloud legacy ExchangeDN to X500"
out-logfile -string $office365DLConfiguration.legacyExchangeDN
$functionEmailAddress = "X500:"+$office365DLConfiguration.legacyExchangeDN
out-logfile -string ("The x500 address to process = "+$functionEmailAddress)
try {
Set-O365UnifiedGroup -identity $functionExternalDirectoryObjectID -emailAddresses @{add=$functionEmailAddress} -errorAction STOP
}
catch {
out-logfile -string ("Error processing address: "+$functionEmailAddress)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Proxy Addresses"
ErrorMessage = ("Address "+$functionEmailAddress+" could not be added to new cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
if ($routingAddressIsPresent -eq $FALSE)
{
out-logfile -string "A hybrid remote routing address was not present. Adding hybrid remote routing address."
$hybridRemoteRoutingAddress=$functionMailNickName+"@"+$mailOnMicrosoftComDomain
out-logfile -string ("Calculated hybrid remote routing address = "+$hybridRemoteRoutingAddress)
out-logfile -string ("Determine if the calculated routing address is already in use.")
$hybridDoLoop = $FALSE
do
{
if (get-o365Recipient -identity $hybridRemoteRoutingAddress)
{
out-logfile -string "Calculated hybrid remote routing address found on another mail enabled object."
$hybridDoLoop = $FALSE
$hybridDoRandom = (Get-Random).toString()
$hybridRemoteRoutingAddress=$functionMailNickName+$hybridDoRandom+"@"+$mailOnMicrosoftComDomain
out-logfile -string ("Calculated remote routing address with random number: "+$hybridRemoteRoutingAddress)
}
else
{
out-logfile -string "Calculated hybrid remote routing address is not present - continue."
$hybridDoLoop = $TRUE
}
}until ($hybridDoLoop -eq $TRUE)
try {
Set-O365UnifiedGroup -identity $functionExternalDirectoryObjectID -emailAddresses @{add=$hybridRemoteRoutingAddress} -errorAction STOP
}
catch {
out-logfile -string ("Error processing address: "+$hybridRemoteRoutingAddress)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Proxy Addresses"
ErrorMessage = ("Address "+$hybridRemoteRoutingAddress+" could not be added to new cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
$isTestError=$FALSE
out-logfile -string "Remove the SMTP Address added by creating the temporary DL."
try {
out-logfile -string ("Removing: "+$functionEmailAddressToRemove)
Set-O365UnifiedGroup -identity $functionExternalDirectoryObjectID -emailAddresses @{remove=$functionEmailAddressToRemove} -errorAction STOP
}
catch {
out-logfile -string "Unable to remove SMTP address assigned by default during group creation."
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Unable to remove temporary SMTP address of group."
ErrorMessage = ("Unable to remove" +$functionEmailAddressToRemove+" - manual removal required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
try
{
out-logfile -string "Remove the migration user from owners which is added by default."
remove-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType "Owner" -links $exchangeOnlineCredential.userPrincipalName -confirm:$FALSE -errorAction STOP
}
catch
{
out-logfile -string "Unable to remove the migration user as an owner."
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Unable to remove the migration administrator as an owner of the group."
ErrorMessage = ("Unable to remove" +$exchangeOnlineCredential.userPrincipalName+" - manual removal from owners required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
try
{
out-logfile -string "Remove the migration user from members which is added by default."
remove-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType "Member" -links $exchangeOnlineCredential.userPrincipalName -confirm:$FALSE -errorAction STOP
}
catch
{
out-logfile -string "Unable to remove the migration user as an member."
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Unable to remove the migration administrator as an member of the group."
ErrorMessage = ("Unable to remove" +$exchangeOnlineCredential.userPrincipalName+" - manual removal from members required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
else
{
out-logfile -string "This is the first pass - update all non-conflicting attributes of the original group."
out-logFile -string "Evaluating exchangeDLMembershipSMTP"
if ($exchangeDLMembershipSMTP -ne $NULL)
{
#All of the members were previously verified as present - so no member should be gone by now unless removed.
#This adds all members as a single operation. Errors we silently continue.
#Ensureing all addresses in the array are unique.
foreach ($member in $exchangeDLMembershipSMTP)
{
if ($member.externalDirectoryObjectID -ne $NULL)
{
out-logfile -string ("Processing directory ID: "+$member.ExternalDirectoryObjectID)
$functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")
$functionRecipients+=$functionDirectoryObjectID[1]
}
else
{
out-logfile -string ("Processing SMTPAddress: "+$member.primarySMTPAddressOrUPN)
$functionRecipients+=$member.primarySMTPAddressOrUPN
}
}
#Becuase groups could have been mirgated and retained - this ensures that all SMTP addresses and GUIDs in the array are unique.
$functionRecipients = $functionRecipients | select-object -Unique
out-logfile -string "Updating membership with unique values."
out-logfile -string $functionRecipients
#Using update to reset the entire membership of the DL to the unique array.
#Alberto Larrinaga for the suggestion.
try {
add-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType Member -Links $functionRecipients -errorAction Stop
}
catch {
out-logfile -string "Unable to bulk update distribution group membership."
out-logfile -string $_
$isTestError=$TRUE
}
if ($isTestError -eq $TRUE)
{
out-logfile -string "Attempting to update membership individually..."
foreach ($recipient in $functionRecipients)
{
out-logfile -string ("Attempting to add recipient: "+$recipient)
try {
add-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType Member -links $recipient -errorAction STOP
}
catch {
out-logfile -string "Error on individual recipient add."
out-logfile -string "It is possible that the operation times out or server returns busy - sleep 15 and retry"
start-sleepProgress -sleepSeconds 15 -sleepString "Sleeping due to error on individual add to retry."
try
{
add-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType Member -links $recipient -errorAction STOP
}
catch
{
out-logfile -string ("Error processing recipient: "+$recipient)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $originalDLConfiguration.mailNickName
Name = $functionMailNickName
Attribute = "Cloud Distribution Group Member"
ErrorMessage = ("Member "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
}
}
}
else
{
Out-LogFile -string "There were no members to process."
}
$isTestError=$FALSE #Resetting error trigger.
$functionRecipients=@() #Reset the test array.
out-logFile -string "Evaluating exchangeRejectMessagesSMTP"
if ($exchangeRejectMessagesSMTP -ne $NULL)
{
foreach ($member in $exchangeRejectMessagesSMTP)
{
#Implement some protections for larger operations to ensure we do not exhaust our powershell budget.
if ($member.isAmbiguous -eq $TRUE)
{
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group RejectMessagesFromSendersOrMembers"
ErrorMessage = ("AMBIGUOUS_RECIPIENT_EXCEPTION: Member of RejectMessagesFromSendersOrMembers "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
elseif ($member.externalDirectoryObjectID -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.externalDirectoryObjectID)
$functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")
out-LogFile -string ("Processing updated member = "+$functionDirectoryObjectID[1])
$functionRecipients+=$functionDirectoryObjectID[1]
}
elseif ($member.primarySMTPAddressOrUPN -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.PrimarySMTPAddressOrUPN)
$functionRecipients+=$member.primarySMTPAddressOrUPN
}
else
{
out-logfile -string "Invalid function object for recipient." -isError:$TRUE
}
}
#Becuase groups could have been mirgated and retained - this ensures that all SMTP addresses and GUIDs in the array are unique.
$functionRecipients = $functionRecipients | select-object -Unique
out-logfile -string "Updating reject messages SMTP with unique values."
out-logfile -string $functionRecipients
out-logfile -string "Testing if recipients contains the migrated group SMTP address - replace with new group SMTP."
out-logfile -string 'This allows all attributes to be set prior to renaming.'
if ($functionRecipients -contains $originalDLConfiguration.mail)
{
$functionIndex = $functionRecipients.indexOf($originalDlConfiguration.mail)
out-logfile -string $functionIndex
out-logfile -string $functionRecipients[$functionIndex]
$functionRecipients[$functionIndex] = $office365DLConfigurationPostMigration.primarySMTPAddress
out-logfile -string $functionRecipients[$functionIndex]
}
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -RejectMessagesFromSendersOrMembers $functionRecipients -errorAction STOP
}
catch {
out-logfile -string "Error bulk updating RejectMessagesFromSendersOrMembers"
out-logfile -string $_
$isTestError=$TRUE
}
if ($isTestError -eq $TRUE)
{
out-logfile -string "Attempting individual update of RejectMessagesFromSendersOrMembers"
foreach ($recipient in $functionRecipients)
{
out-logfile -string ("Attempting to add recipient: "+$recipient)
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -RejectMessagesFromSendersOrMembers @{Add=$recipient} -errorAction STOP
}
catch {
out-logfile -string ("Error processing recipient: "+$recipient)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group RejectMessagesFromSendersOrMembers"
ErrorMessage = ("DL_PROPERTY_UPDATE_EXCEPTION: Member of RejectMessagesFromSendersOrMembers "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
}
}
else
{
Out-LogFile -string "There were no members to process."
}
$isTestError = $FALSE #Reset error tracker.
$functionRecipients=@() #Reset the test array.
out-logFile -string "Evaluating exchangeAcceptMessagesSMTP"
if ($exchangeAcceptMessageSMTP -ne $NULL)
{
foreach ($member in $exchangeAcceptMessageSMTP)
{
#Implement some protections for larger operations to ensure we do not exhaust our powershell budget.
if ($member.isAmbiguous -eq $TRUE)
{
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group AcceptMessagesOnlyFromSendersOrMembers"
ErrorMessage = ("AMBIGUOUS_RECIPIENT_EXCEPTION: Member of AcceptMessagesOnlyFromSendersOrMembers "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
if ($member.externalDirectoryObjectID -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.externalDirectoryObjectID)
$functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")
out-LogFile -string ("Processing updated member = "+$functionDirectoryObjectID[1])
$functionRecipients+=$functionDirectoryObjectID[1]
}
elseif ($member.primarySMTPAddressOrUPN -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.PrimarySMTPAddressOrUPN)
$functionRecipients+=$member.primarySMTPAddressOrUPN
}
else
{
out-logfile -string "Invalid function object for recipient." -isError:$TRUE
}
}
#Becuase groups could have been mirgated and retained - this ensures that all SMTP addresses and GUIDs in the array are unique.
$functionRecipients = $functionRecipients | select-object -Unique
out-logfile -string "Updating accept messages SMTP with unique values."
out-logfile -string $functionRecipients
out-logfile -string "Testing if recipients contains the migrated group SMTP address - replace with new group SMTP."
out-logfile -string 'This allows all attributes to be set prior to renaming.'
if ($functionRecipients -contains $originalDLConfiguration.mail)
{
$functionIndex = $functionRecipients.indexOf($originalDlConfiguration.mail)
out-logfile -string $functionIndex
out-logfile -string $functionRecipients[$functionIndex]
$functionRecipients[$functionIndex] = $office365DLConfigurationPostMigration.primarySMTPAddress
out-logfile -string $functionRecipients[$functionIndex]
}
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -AcceptMessagesOnlyFromSendersOrMembers $functionRecipients -errorAction STOP
}
catch {
out-logfile -string "Error bulk updating AcceptMessagesOnlyFromSendersOrMembers."
out-logfile -string $_
$isTestError = $TRUE
}
if ($isTestError -eq $TRUE)
{
out-logfile -string "Attempting individual update of AcceptMessagesOnlyFromSendersOrMembers"
foreach ($recipient in $functionRecipients)
{
out-logfile -string ("Attempting to add recipient: "+$recipient)
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -AcceptMessagesOnlyFromSendersOrMembers @{Add=$recipient} -errorAction STOP
}
catch {
out-logfile -string ("Error processing recipient: "+$recipient)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group AcceptMessagesOnlyFromSendersOrMembers"
ErrorMessage = ("Member of AcceptMessagesOnlyFromSendersOrMembers "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
}
}
else
{
Out-LogFile -string "There were no members to process."
}
$isTestError = $FALSE #Reset error tracker.
$functionRecipients=@() #Reset the test array.
out-logFile -string "Evaluating exchangeManagedBySMTP"
if ($exchangeManagedBySMTP -ne $NULL)
{
foreach ($member in $exchangeManagedBySMTP)
{
#Implement some protections for larger operations to ensure we do not exhaust our powershell budget.
if ($member.isAmbiguous -eq $TRUE)
{
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group ManagedBy"
ErrorMessage = ("AMBIGUOUS_RECIPIENT_EXCEPTION: Member of ManagedBy "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
elseif ($member.externalDirectoryObjectID -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.externalDirectoryObjectID)
$functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")
out-LogFile -string ("Processing updated member = "+$functionDirectoryObjectID[1])
$functionRecipients+=$functionDirectoryObjectID[1]
}
elseif ($member.primarySMTPAddressOrUPN -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.PrimarySMTPAddressOrUPN)
$functionRecipients+=$member.primarySMTPAddressOrUPN
}
else
{
out-logfile -string "Invalid function object for recipient." -isError:$TRUE
}
}
#Becuase groups could have been mirgated and retained - this ensures that all SMTP addresses and GUIDs in the array are unique.
$functionRecipients = $functionRecipients | select-object -Unique
out-logfile -string "Updating managed by SMTP with unique values."
out-logfile -string $functionRecipients
out-logfile -string "Testing if recipients contains the migrated group SMTP address - replace with new group SMTP."
out-logfile -string 'This allows all attributes to be set prior to renaming.'
if ($functionRecipients -contains $originalDLConfiguration.mail)
{
$functionIndex = $functionRecipients.indexOf($originalDlConfiguration.mail)
out-logfile -string $functionIndex
out-logfile -string $functionRecipients[$functionIndex]
$functionRecipients[$functionIndex] = $office365DLConfigurationPostMigration.primarySMTPAddress
out-logfile -string $functionRecipients[$functionIndex]
}
try {
add-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType Owners -links $functionRecipients -errorAction STOP
}
catch {
out-logfile -string "Unable to bulk update managedBy"
out-logfile $_
$isTestError=$TRUE
}
if ($isTestError -eq $TRUE)
{
out-logfile -string "Attempting individual update of ManagedBy"
foreach ($recipient in $functionRecipients)
{
out-logfile -string ("Attempting to add recipient: "+$recipient)
try {
add-o365UnifiedGroupLinks -identity $functionExternalDirectoryObjectID -linkType Owners -links $recipient -errorAction STOP
}
catch {
out-logfile -string ("Error processing recipient: "+$recipient)
out-logfile -string $_
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group ManagedBy"
ErrorMessage = ("Member of ManagedBy "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
}
}
}
else
{
Out-LogFile -string "There were no members to process."
}
$isTestError = $FALSE #Reset error tracker.
$functionRecipients=@() #Reset the test array.
out-logFile -string "Evaluating exchangeModeratedBy"
if ($exchangeModeratedBySMTP -ne $NULL)
{
foreach ($member in $exchangeModeratedBySMTP)
{
#Implement some protections for larger operations to ensure we do not exhaust our powershell budget.
if ($member.isAmbiguous -eq $TRUE)
{
$isErrorObject = new-Object psObject -property @{
PrimarySMTPAddressorUPN = $originalDLConfiguration.mail
ExternalDirectoryObjectID = $office365DLConfiguration.externalDirectoryObjectID
Alias = $functionMailNickName
Name = $originalDLConfiguration.name
Attribute = "Cloud Distribution Group ModeratedBy"
ErrorMessage = ("AMBIGUOUS_RECIPIENT_EXCEPTION: Member of ModeratedBy "+$recipient+" unable to add to cloud distribution group. Manual addition required.")
ErrorMessageDetail = $_
}
out-logfile -string $isErrorObject
$functionErrors+=$isErrorObject
}
elseif ($member.externalDirectoryObjectID -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.externalDirectoryObjectID)
$functionDirectoryObjectID=$member.externalDirectoryObjectID.Split("_")
out-LogFile -string ("Processing updated member = "+$functionDirectoryObjectID[1])
$functionRecipients+=$functionDirectoryObjectID[1]
}
elseif ($member.primarySMTPAddressOrUPN -ne $NULL)
{
out-LogFile -string ("Processing member = "+$member.PrimarySMTPAddressOrUPN)
$functionRecipients+=$member.primarySMTPAddressOrUPN
}
else
{
out-logfile -string "Invalid function object for recipient." -isError:$TRUE
}
}
#Becuase groups could have been mirgated and retained - this ensures that all SMTP addresses and GUIDs in the array are unique.
$functionRecipients = $functionRecipients | select-object -Unique
out-logfile -string "Updating moderated by SMTP with unique values."
out-logfile -string $functionRecipients
out-logfile -string "Testing if recipients contains the migrated group SMTP address - replace with new group SMTP."
out-logfile -string 'This allows all attributes to be set prior to renaming.'
if ($functionRecipients -contains $originalDLConfiguration.mail)
{
$functionIndex = $functionRecipients.indexOf($originalDlConfiguration.mail)
out-logfile -string $functionIndex
out-logfile -string $functionRecipients[$functionIndex]
$functionRecipients[$functionIndex] = $office365DLConfigurationPostMigration.primarySMTPAddress
out-logfile -string $functionRecipients[$functionIndex]
}
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -moderatedBy $functionRecipients -errorAction STOP
}
catch {
out-logfile -string "Unable to bulk update moderatedBy."
out-logfile -string $_
$isTestError=$TRUE
}
if ($isTestError -eq $TRUE)
{
out-logfile -string "Attempting individual update of ModeratedBy"
foreach ($recipient in $functionRecipients)
{
out-logfile -string ("Attempting to add recipient: "+$recipient)
try {
set-o365UnifiedGroup -identity $functionExternalDirectoryObjectID -moderatedBy @{Add=$recipient} -errorAction STOP
}
catch {