-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-MultipleMachineContactMigration.ps1
1105 lines (864 loc) · 47.5 KB
/
start-MultipleMachineContactMigration.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
#############################################################################################
# DISCLAIMER: #
# #
# THE SAMPLE SCRIPTS ARE NOT SUPPORTED UNDER ANY MICROSOFT STANDARD SUPPORT #
# PROGRAM OR SERVICE. THE SAMPLE SCRIPTS ARE PROVIDED AS IS WITHOUT WARRANTY #
# OF ANY KIND. MICROSOFT FURTHER DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING, WITHOUT #
# LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR OF FITNESS FOR A PARTICULAR #
# PURPOSE. THE ENTIRE RISK ARISING OUT OF THE USE OR PERFORMANCE OF THE SAMPLE SCRIPTS #
# AND DOCUMENTATION REMAINS WITH YOU. IN NO EVENT SHALL MICROSOFT, ITS AUTHORS, OR #
# ANYONE ELSE INVOLVED IN THE CREATION, PRODUCTION, OR DELIVERY OF THE SCRIPTS BE LIABLE #
# FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS #
# PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) #
# ARISING OUT OF THE USE OF OR INABILITY TO USE THE SAMPLE SCRIPTS OR DOCUMENTATION, #
# EVEN IF MICROSOFT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES #
#############################################################################################
Function Start-MultipleMachineDistributionListMigration
{
<#
.SYNOPSIS
This is the wrapper function that provisions jobs for multiple distribution list migrations.
.DESCRIPTION
This is the wrapper function that provisions jobs for multiple distribution list migrations.
.PARAMETER contactSMTPAddresses
*REQUIRED*
This is the array of distribution lists to be migrated
.PARAMETER globalCatalogServer
*REQUIRED*
A global catalog server in the domain where the contact to be migrated resides.
.PARAMETER activeDirectoryCredential
*REQUIRED*
This is the credential that will be utilized to perform operations against the global catalog server.
If the contact and all it's dependencies reside in a single domain - a domain administrator is acceptable.
If the contact and it's dependencies span multiple domains in a forest - enterprise administrator is required.
.PARAMETER logFolder
*REQUIRED*
The location where logging for the migration should occur including all XML outputs for backups.
.PARAMETER aadConnectServer
*OPTIONAL*
This is the AADConnect server that automated sycn attempts will be attempted.
If specified with an AADConnect credential - delta syncs will be triggered automatically in attempts to service the move.
This requires WINRM be enabled on the ADConnect server and may have additional WINRM dependencies / configuration.
Name should be specified in fully qualified domain format.
.PARAMETER aadConnectCredential
*OPTIONAL*
The credential specified to perform remote powershell / winrm sessions to the AADConnect server.
.PARAMETER exchangeServer
*REQUIRED IF HYBRID MAIL FLOW ENALBED*
This is the on-premises Exchange server that is required for enabling hybrid mail flow if the option is specified.
If using a load balanced namespace - basic authentication on powershell must be enabled on all powersell virtual directories.
If using a single server (direct connection) then kerberos authentication may be utilized.
.PARAMETER exchangeCredential
*REQUIRED IF HYBRID MAIL FLOW ENABLED*
This is the credential utilized to establish remote powershell sessions to Exchange on-premises.
This acccount requires Exchange Organization Management rights in order to enable hybrid mail flow.
.PARAMETER exchangeOnlineCredential
*REQUIRED IF NO OTHER CREDENTIALS SPECIFIED*
This is the credential utilized for Exchange Online connections.
The credential must be specified if certificate based authentication is not configured.
The account requires global administration rights / exchange organization management rights.
An exchange online credential cannot be combined with an exchangeOnlineCertificateThumbprint.
.PARAMETER exchangeOnlineCertificateThumbprint
*REQUIRED IF NO OTHER CREDENTIALS SPECIFIED*
This is the certificate thumbprint that will be utilzied for certificate authentication to Exchange Online.
This requires all the pre-requists be established and configured prior to access.
A certificate thumbprint cannot be specified with exchange online credentials.
.PARAMETER exchangeAuthenticationMethod
*OPTIONAL*
This allows the administrator to specify either Kerberos or Basic authentication for on premises Exchange Powershell.
Basic is the assumed default and requires basic authentication be enabled on the powershell virtual directory of the specified exchange server.
.PARAMETER retainOffice365Settings
*OPTIONAL*
It is possible over the course of migrations that cloud only resources could have dependencies on objects that still remain on premises.
The administrator can choose to scan office 365 to capture any cloud only dependencies that may exist.
The default is true.
.PARAMETER doNoSyncOU
*REQUIRED IF RETAIN contact FALSE*
This is the administrator specified organizational unit that is NOT configured to sync in AD Connect.
When the administrator specifies to NOT retain the contact the contact is moved to this OU to allow for deletion from Office 365.
A doNOSyncOU must be specified if the administrator specifies to NOT retain the contact.
.PARAMETER retainOriginalContact
*OPTIONAL*
Allows the administrator to retain the contact - for example if the contact also has on premises security dependencies.
This triggers a mail disable of the contact resulting in contact deletion from Office 365.
The name of the contact is randomized with a character ! to ensure no conflict with hybird mail flow - if hybrid mail flow enabled.
.PARAMETER enableHybridMailFlow
*OPTIONAL*
Allows the administrator to decide that they want mail flow from on premises to cloud to work for the migrated contact.
This involves provisioning a mail contact and a dynamic distribution contact.
The dynamic distribution contact is intentionally choosen to prevent soft matching of a contact and an undo of the migration.
This option requires on premises Exchange be specified and configured.
.PARAMETER contactTypeOverride
*OPTIONAL*
This allows the administrator to override the contact type created in the cloud from on premises.
For example - if the contact was provisioned on premises as security but does not require security rights in Office 365 - the administrator can override to DISTRIBUTION.
Mandatory types -> SECURITY or DISTRIBUTION
.OUTPUTS
Logs all activities and backs up all original data to the log folder directory.
Moves the distribution contact from on premieses source of authority to office 365 source of authority.
.EXAMPLE
Start-DistributionListMigration
#>
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
[array]$contactSMTPAddresses,
[Parameter(Mandatory = $true)]
[string]$globalCatalogServer,
[Parameter(Mandatory = $true)]
[array]$activeDirectoryCredential,
[Parameter(Mandatory = $true)]
[string]$logFolderPath,
[Parameter(Mandatory = $false)]
[string]$aadConnectServer=$NULL,
[Parameter(Mandatory = $false)]
[array]$aadConnectCredential=$NULL,
[Parameter(Mandatory = $false)]
[string]$exchangeServer=$NULL,
[Parameter(Mandatory = $false)]
[array]$exchangeCredential=$NULL,
[Parameter(Mandatory = $false)]
[array]$exchangeOnlineCredential=$NULL,
[Parameter(Mandatory = $false)]
[string]$exchangeOnlineCertificateThumbPrint="",
[Parameter(Mandatory = $false)]
[string]$exchangeOnlineOrganizationName="",
[Parameter(Mandatory = $false)]
[ValidateSet("O365Default","O365GermanyCloud","O365China","O365USGovGCCHigh","O365USGovDoD")]
[string]$exchangeOnlineEnvironmentName="O365Default",
[Parameter(Mandatory = $false)]
[string]$exchangeOnlineAppID="",
[Parameter(Mandatory = $false)]
[ValidateSet("Basic","Kerberos")]
[string]$exchangeAuthenticationMethod="Basic",
[Parameter(Mandatory = $false)]
[boolean]$retainOffice365Settings=$true,
[Parameter(Mandatory = $true)]
[string]$dnNoSyncOU = "NotSet",
[Parameter(Mandatory = $false)]
[boolean]$retainOriginalContact = $TRUE,
[Parameter(Mandatory = $false)]
[boolean]$enableHybridMailflow = $FALSE,
[Parameter(Mandatory = $false)]
[ValidateSet("Security","Distribution")]
[string]$contactTypeOverride="None",
[Parameter(Mandatory = $false)]
[boolean]$triggerUpgradeToOffice365contact=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$retainSendAsOffice365=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$useCollectedFullMailboxAccessOnPrem=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$useCollectedFullMailboxAccessOffice365=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$useCollectedSendAsOnPrem=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$useCollectedFolderPermissionsOnPrem=$FALSE,
[Parameter(Mandatory = $false)]
[boolean]$useCollectedFolderPermissionsOffice365=$FALSE,
[Parameter(Mandatory = $TRUE)]
[array]$serverNames = $NULL,
[Parameter(Mandatory = $TRUE)]
[string]$remoteDriveLetter=$NULL
)
$windowTitle = "Start-MultipleMachineDistributionListMigration Controller"
$host.ui.RawUI.WindowTitle = $windowTitle
#Define global variables.
$global:logFile=$NULL #This is the global variable for the calculated log file name
[string]$global:staticFolderName="\Controller\"
[string]$masterFileName="Controller"
#Define parameters that are variables here (not available as parameters in this function.)
[boolean]$retainSendAsOnPrem=$FALSE
[boolean]$retainFullMailboxAccessOnPrem=$FALSE
[boolean]$retainMailboxFolderPermsOnPrem=$FALSE
[boolean]$retainFullMailboxAccessOffice365=$FALSE
[boolean]$retainMailboxFolderPermsOffice365=$FALSE
$jobOutput=$NULL
[int]$totalAddressCount = 0
[int]$maxThreadCount = 5
[string]$contactConversionV2ModuleName = "contactConversionV2"
[string]$localHostName=$NULL
new-LogFile -contactSMTPAddress $masterFileName -logFolderPath $logFolderPath
Out-LogFile -string "================================================================================"
Out-LogFile -string "BEGIN START-MULTIPLEMACHINEDISTRIBUTIONLISTMIGRATION"
Out-LogFile -string "================================================================================"
#Add immediate check to ensure controller is running as administrator.
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
[boolean]$adminTest = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($adminTest -eq $FALSE)
{
out-logfile -string "Please run powershell as administrator." -isError:$TRUE
}
#Output parameters to the log file for recording.
#For parameters that are optional if statements determine if they are populated for recording.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "PARAMETERS"
Out-LogFile -string "********************************************************************************"
out-logfile -string "SMTP Addresses:"
foreach ($smtpAddress in $contactSMTPAddresses)
{
Out-LogFile -string $smtpAddress
}
out-logfile -string "Servers to Excute On:"
foreach ($server in $serverNames)
{
Out-LogFile -string $server
}
Out-LogFile -string ("GlobalCatalogServer = "+$globalCatalogServer)
#Out-LogFile -string ("ActiveDirectoryUserName = "+$activeDirectoryCredential.UserName.tostring())
Out-LogFile -string ("LogFolderPath = "+$logFolderPath)
if ($aadConnectServer -ne "")
{
Out-LogFile -string ("AADConnectServer = "+$aadConnectServer)
}
if ($aadConnectCredential -ne $null)
{
Out-LogFile -string ("AADConnectUserName = "+$aadConnectCredential.UserName.tostring())
}
if ($exchangeServer -ne "")
{
Out-LogFile -string ("ExchangeServer = "+$exchangeServer)
}
if ($exchangecredential -ne $null)
{
Out-LogFile -string ("ExchangeUserName = "+$exchangeCredential.UserName.toString())
}
if ($exchangeOnlineCredential -ne $null)
{
Out-LogFile -string ("ExchangeOnlineUserName = "+ $exchangeOnlineCredential.UserName.toString())
}
if ($exchangeOnlineCertificateThumbPrint -ne "")
{
Out-LogFile -string ("ExchangeOnlineCertificateThumbprint = "+$exchangeOnlineCertificateThumbPrint)
}
Out-LogFile -string ("ExchangeAuthenticationMethod = "+$exchangeAuthenticationMethod)
out-logfile -string ("Retain Office 365 Settings = "+$retainOffice365Settings)
out-logfile -string ("OU that does not sync to Office 365 = "+$dnNoSyncOU)
out-logfile -string ("Will the original contact be retained as part of migration = "+$retainOriginalContact)
out-logfile -string ("Enable hybrid mail flow = "+$enableHybridMailflow)
out-logfile -string ("contact type override = "+$contactTypeOverride)
out-logfile -string ("Trigger upgrade to Office 365 contact = "+$triggerUpgradeToOffice365contact)
out-logfile -string ("Retain full mailbox access on premises = "+$retainFullMailboxAccessOnPrem)
out-logfile -string ("Retain send as rights on premise = "+$retainSendAsOnPrem)
out-logfile -string ("Retain mailbox folder permissions on premises = "+$retainMailboxFolderPermsOnPrem)
out-logfile -string ("Retain full mailbox access Office 365 = "+$retainFullMailboxAccessOffice365)
out-logfile -string ("Retain send as rights Office 365 = "+$retainSendAsOffice365)
out-logfile -string ("Retain mailbox folder permissions Office 365 = "+$retainMailboxFolderPermsOffice365)
out-logfile -string ("Use collected full mailbox permissions on premises = "+$useCollectedFullMailboxAccessOnPrem)
out-logfile -string ("Use collected full mailbox permissions Office 365 ="+$useCollectedFullMailboxAccessOffice365)
out-logfile -string ("Use collected send as on premsies = "+$useCollectedSendAsOnPrem)
out-logfile -string ("Use colleced mailbox folder permissions on premises = "+$useCollectedFolderPermissionsOnPrem)
out-logfile -string ("Use collected mailbox folder permissions Office 365 = "+$useCollectedFolderPermissionsOffice365)
Out-LogFile -string "********************************************************************************"
#Perform paramter validation manually.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "ENTERING PARAMTER VALIDATION"
Out-LogFile -string "********************************************************************************"
#Test to ensure that if any of the aadConnect parameters are passed - they are passed together.
out-logfile -string "Validating that the remote network drive passed is a single valid drive letter"
if ($remoteDriveLetter -eq $NULL)
{
out-logfile -string "A remote drive letter is required - specify S for example." -isError:$TRUE
}
else
{
if ($remoteDriveLetter.count -gt 1)
{
out-logfile -string "Please specify a single drive letter - for example S" -isError:$TRUE
}
else
{
out-logfile -string "Drive letter specified is a single character."
if ([regex]::Match($remoteDriveLetter,"[a-zA-Z]"))
{
out-logfile -string "Drive letter specified is single and is a valid drive character."
}
else
{
out-logfile -string "Please specify a valid character A-Z or a-z for the remote drive letter." -iserror:$TRUE
}
}
}
Out-LogFile -string "Validating that both AADConnectServer and AADConnectCredential are specified"
if (($aadConnectServer -eq "") -and ($aadConnectCredential -ne $null))
{
#The credential was specified but the server name was not.
Out-LogFile -string "ERROR: AAD Connect Server is required when specfying AAD Connect Credential" -isError:$TRUE
}
elseif (($aadConnectCredential -eq $NULL) -and ($aadConnectServer -ne ""))
{
#The server name was specified but the credential was not.
Out-LogFile -string "ERROR: AAD Connect Credential is required when specfying AAD Connect Server" -isError:$TRUE
}
elseif (($aadConnectCredential -ne $NULL) -and ($aadConnectServer -ne ""))
{
#The server name and credential were specified for AADConnect.
Out-LogFile -string "AADConnectServer and AADConnectCredential were both specified."
foreach ($credential in $aadConnectCredential)
{
if ($credential.gettype().name -eq "PSCredential")
{
out-logfile -string ("Tested credential: "+$credential.userName)
}
else
{
out-logfile -string "ADConnect credential not valid.. All credentials must be PSCredential types." -isError:$TRUE
}
}
if ($aadConnectCredential.count -lt $serverNames.count)
{
out-logfile -string "ERROR: Must specify one ad connect credential for each migratione server." -isError:$TRUE
}
else
{
out-logfile -string "The number of ad connect credentials matches the server count."
}
}
#Validate that both the exchange credential and exchange server are presented together.
Out-LogFile -string "Validating that both ExchangeServer and ExchangeCredential are specified."
if (($exchangeServer -eq "") -and ($exchangeCredential -ne $null))
{
#The exchange credential was specified but the exchange server was not specified.
Out-LogFile -string "ERROR: Exchange Server is required when specfying Exchange Credential." -isError:$TRUE
}
elseif (($exchangeCredential -eq $NULL) -and ($exchangeServer -ne ""))
{
#The exchange server was specified but the exchange credential was not.
Out-LogFile -string "ERROR: Exchange Credential is required when specfying Exchange Server." -isError:$TRUE
}
elseif (($exchangeCredential -ne $NULL) -and ($exchangetServer -ne ""))
{
#The server name and credential were specified for Exchange.
Out-LogFile -string "The server name and credential were specified for Exchange."
foreach ($credential in $exchangecredential)
{
if ($credential.gettype().name -eq "PSCredential")
{
out-logfile -string ("Tested credential: "+$credential.userName)
}
else
{
out-logfile -string "Exchange credential not valid.. All credentials must be PSCredential types." -isError:$TRUE
}
}
if ($exchangeCredential.count -lt $serverNames.count)
{
out-logfile -string "ERROR: Must specify one exchange credential for each migratione server." -isError:$TRUE
}
else
{
out-logfile -string "The number of exchange credentials matches the server count."
}
}
else
{
Out-LogFile -string ("Neither Exchange Server or Exchange Credentials specified - retain useOnPremisesExchange FALSE - "+$useOnPremisesExchange)
}
#Validate that only one method of engaging exchange online was specified.
Out-LogFile -string "Validating Exchange Online Credentials."
if (($exchangeOnlineCredential -ne $NULL) -and ($exchangeOnlineCertificateThumbPrint -ne ""))
{
Out-LogFile -string "ERROR: Only one method of cloud authentication can be specified. Use either cloud credentials or cloud certificate thumbprint." -isError:$TRUE
}
elseif (($exchangeOnlineCredential -eq $NULL) -and ($exchangeOnlineCertificateThumbPrint -eq ""))
{
out-logfile -string "ERROR: One permissions method to connect to Exchange Online must be specified." -isError:$TRUE
}
else
{
Out-LogFile -string "Only one method of Exchange Online authentication specified."
}
#Validate that all information for the certificate connection has been provieed.
if (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "The exchange organiztion name and application ID are required when using certificate thumbprint authentication to Exchange Online." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -ne "") -and ($exchangeOnlineAppID -eq ""))
{
out-logfile -string "The exchange application ID is required when using certificate thumbprint authentication." -isError:$TRUE
}
elseif (($exchangeOnlineCertificateThumbPrint -ne "") -and ($exchangeOnlineOrganizationName -eq "") -and ($exchangeOnlineAppID -ne ""))
{
out-logfile -string "The exchange organization name is required when using certificate thumbprint authentication." -isError:$TRUE
}
else
{
out-logfile -string "All components necessary for Exchange certificate thumbprint authentication were specified."
}
#Validate that an OU was specified <if> retain contact is not set to true.
Out-LogFile -string "Validating that if retain original contact is false a non-sync OU is specified."
if (($retainOriginalContact -eq $FALSE) -and ($dnNoSyncOU -eq "NotSet"))
{
out-LogFile -string "A no SYNC OU is required if retain original contact is false." -isError:$TRUE
}
if (($useOnPremisesExchange -eq $False) -and ($enableHybridMailflow -eq $true))
{
out-logfile -string "Exchange on premsies information must be provided in order to enable hybrid mail flow." -isError:$TRUE
}
if ($useCollectedFullMailboxAccessOnPrem -eq $TRUE)
{
$retainFullMailboxAccessOnPrem=$TRUE
}
if ($useCollectedFullMailboxAccessOffice365 -eq $TRUE)
{
$retainFullMailboxAccessOffice365=$TRUE
}
if ($useCollectedSendAsOnPrem -eq $TRUE)
{
$retainSendAsOnPrem=$TRUE
}
if ($useCollectedFolderPermissionsOnPrem -eq $TRUE)
{
$retainMailboxFolderPermsOnPrem=$TRUE
}
if ($useCollectedFolderPermissionsOffice365 -eq $TRUE)
{
$retainMailboxFolderPermsOffice365=$TRUE
}
if (($retainMailboxFolderPermsOffice365 -eq $TRUE) -and ($useCollectedFolderPermissionsOffice365 -eq $FALSE))
{
out-logfile -string "In order to retain folder permissions of migrated distribution lists the collection functions / files must first exist and be utilized." -isError:$TRUE
}
if (($retainOnPremMailboxFolderPermissions -eq $TRUE) -and ($useCollectedFolderPermissionsOnPrem -eq $FALSE))
{
out-logfile -string "In order to retain folder permissions of migrated distribution lists the collection functions / files must first exist and be utilized." -isError:$TRUE
}
out-logfile -string "Validating the active directory credential array contains all PSCredentials."
foreach ($credential in $activeDirectoryCredential)
{
if ($credential.gettype().name -eq "PSCredential")
{
out-logfile -string ("Tested credential: "+$credential.userName)
}
else
{
out-logfile -string "Active directory credential not valid. All credentials must be PSCredential types." -isError:$TRUE
}
}
out-logfile -string "Validating the exchange online credential array"
foreach ($credential in $exchangeOnlineCredential)
{
if ($credential.gettype().name -eq "PSCredential")
{
out-logfile -string ("Tested credential: "+$credential.userName)
}
else
{
out-logfile -string "Exchange online credential not valid.. All credentials must be PSCredential types." -isError:$TRUE
}
}
if ($serverNames.count -gt 5)
{
out-logfile -string "More than 5 migration servers were specified. The current limit is 5 servers." -isError:$TRUE
}
if ($activeDirectoryCredential.count -lt $serverNames.count)
{
out-logfile -string "ERROR: Must specify one active directory credential for each migration server." -isError:$TRUE
}
else
{
out-logfile -string "The number of active directory credentials matches the server count."
}
if ($exchangeOnlineCredential.count -lt $serverNames.count)
{
out-logfile -string "ERROR: Must specify one exchange online credential for each migratione server." -isError:$TRUE
}
else
{
out-logfile -string "The number of exchange online credentials matches the server count."
}
Out-LogFile -string "END PARAMETER VALIDATION"
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "The following SMTP addresses have been requested for migration."
#Ensure that no addresses are specified more than once.
out-logfile -string "Unique list of SMTP addresses included in the array."
$contactSMTPAddresses = $contactSMTPAddresses | Select-Object -Unique
#Setting the total contact count after adjusting for unique SMTP Addresses.
$totalAddressCount = $contactSMTPAddresses.count
foreach ($contactSMTPAddress in $contactSMTPAddresses)
{
out-logfile -string $contactSMTPAddress
}
#At this time we need to record the FQDN of the local host. This is used later to determine if jobs are local.
$localHostName = ([System.Net.Dns]::GetHostByName(($env:computerName))).hostname
out-logfile -string ("The local host name is = "+$localHostName)
foreach ($server in $serverNames)
{
out-logfile -string ("Server Specified: "+$server)
}
#Servers must be specified in FQDN format. Although no specific way to test - an easy method is to break the string at . and count.
#If the count is not > 3 machine <dot> domain <dot> com then reasonably this is not an FQDN.
foreach ($server in $servernames)
{
$forTest = $server.split(".")
if ($forTest.count -lt 3)
{
out-logfile -string ("The servername specified does not appear in FQDN format - "+$server)
out-logfile -string ("The servername must be in format machine.domain.com etc to proceed.") -isError:$TRUE
}
else
{
out-logfile -string ("The servername appears in FQDN format - proceed - "+$server)
}
}
#The goal of this function is to provision remote jobs.
#Test to ensure each machine is configured for remote management.
foreach ($server in $serverNames)
{
if ($server -ne $localHostName)
{
try{
out-logfile -string ("Testing server: "+$server)
#$testResults = test-wsman -computerName $server -authentication Default -credential $activeDirectoryCredential[0] -errorAction STOP
#Removed the credential - if the user has not logged in and has no user profile the credential fails.
$testResults = test-wsman -computerName $server -errorAction STOP
}
catch{
out-logfile -string "Unable to validate remote management enabled on host."
out-logfile -string $server
out-logfile -string $_ -isError:$TRUE
}
}
else
{
out-logfile -string ("Skipping testing of host running controoler: "+$server)
}
}
#For each machine in the server name array - we need to validate that the contactConversionV2 commands are available.
[array]$invalidServers=@()
foreach ($server in $serverNames)
{
[array]$commands = @()
out-logfile -string ("Testing server for presence of contactConversion V2 "+$server)
if ($server -eq $localHostName)
{
out-logfile -string "Skipping test - this is the machine running the controller."
}
else
{
try
{
$commands = invoke-command -scriptBlock {get-command -module $contactConversionV2ModuleName -errorAction STOP} -computerName $server -credential $activeDirectoryCredential[0] -errorAction STOP
if ($commands.count -eq 0)
{
out-logfile -string ("Server "+$server+" does not have the contactConversionV2 module installed.")
$invalidServers+=$server
}
else {
out-logfile -string ("Server "+$server+" is ready.")
}
}
catch{
out-logfile -string "Unable to obtain contactConversionV2 commands."
out-logfile -string $_ -isError:$TRUE
}
}
}
if ($invalidServers.count -gt 0)
{
foreach ($server in $invalidServers)
{
out-logfile -string ("Server does not have contactConversionV2 installed: "+$server)
}
out-logfile -string "Correct servers with missing modules." -isError:$TRUE
}
#At this point we have validated that each server has WinRM enabled, each server is accessible, and each server has the module installed.
#The goal on the controller is to store all of the log files - centralized.
#This requires a network share.
#The share should be pre-created.
$account = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
if (get-SMBShare -name $contactConversionV2ModuleName -errorAction SilentlyContinue)
{
out-logfile -string "The contactConversionV2 share was found."
if ((get-smbShare -name $contactConversionV2ModuleName).path -eq $logFolderPath)
{
out-logfile -string "The contactConversionV2 share was found and the path matches the logging path."
}
else
{
out-logfile -string "The contactConversionV2 share was found but the path does not match the logging path."
out-logfile -string ((get-smbShare -name $contactConversionV2ModuleName).path) -isError:$TRUE
}
}
else
{
try{
out-logfile -string "Creating contactConversionV2 to share to support centralized logging."
new-SMBShare -name $contactConversionV2ModuleName -path $logFolderPath -fullAccess $account -errorAction STOP
}
catch{
out-logfile -string "Uanble to create the contactConversionV2 share."
out-logfile $_ -isError:$TRUE
}
}
#Ensure the ACL is set on the folder for the active directory admin account.
#Do not assume that just becuase the share existed the folder permissions are ok.
try{
foreach ($credential in $activeDirectoryCredential)
{
out-logfile -string "Setting the ACL on the folder for full control to the active directory credential and enabling inheritance."
$acl = Get-Acl $logFolderPath
out-logfile -string $acl
$permission = $credential.userName, "FullControl", 'ContainerInherit, ObjectInherit', 'None', 'Allow'
out-logfile -string $permission
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule -argumentList $permission
out-logfile -string $AccessRule
$acl.SetAccessRule($AccessRule)
out-logfile -string $acl
$acl | Set-Acl $logFolderPath -errorAction STOP -confirm:$FALSE
}
foreach ($server in $servernames)
{
out-logfile -string "Setting the ACL on the folder for full control for machine accounts to the active directory credential and enabling inheritance."
$forServerName = $server.split(".")
$forSamAccountName = get-adComputer -identity $forServerName[0] -server $globalCatalogServer -Credential $activeDirectoryCredential[0]
$acl = Get-Acl $logFolderPath
out-logfile -string $acl
$permission = $forSamAccountName.SAMAccountName, "FullControl", 'ContainerInherit, ObjectInherit', 'None', 'Allow'
out-logfile -string $permission
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule -argumentList $permission
out-logfile -string $AccessRule
$acl.SetAccessRule($AccessRule)
out-logfile -string $acl
$acl | Set-Acl $logFolderPath -errorAction STOP -confirm:$FALSE
}
}
Catch{
out-logfile -string "Unable to set the ACL on the folder for the active directory credential."
out-logfile -string $_ -isError:$TRUE
}
#Do not assume the share was established with permissions for the active directory credentials.
try{
foreach ($credential in $activeDirectoryCredential)
{
Grant-SmbShareAccess -Name $contactConversionV2ModuleName -AccountName $credential.UserName -AccessRight Full -errorACTION STOP -force
$shareAccess = get-SMBShareAccess -name $contactConversionV2ModuleName
out-logfile -string $shareAccess
}
foreach ($server in $serverNames)
{
$forServerName = $server.split(".")
$forSamAccountName = get-adComputer -identity $forServerName[0] -server $globalCatalogServer -Credential $activeDirectoryCredential[0]
Grant-SmbShareAccess -Name $contactConversionV2ModuleName -AccountName $forSAMAccountName.SAMAccountName -AccessRight Full -errorACTION STOP -force
$shareAccess = get-SMBShareAccess -name $contactConversionV2ModuleName
out-logfile -string $shareAccess
}
}
catch{
out-logfile -string "Error granting active directory administrator share access."
out-logfile -string $_ -isError:$TRUE
}
#The share path has been validated. The custom logging directories need to be built.
out-logfile -string "Build each servers network logging directory."
[array]$networkLoggingDirectory=@()
foreach ($server in $servernames)
{
$forServerName=$server.split(".")
$forPath = "\\"+$localHostName+"\"
out-logfile -string $forPath
$forPath=$forPath+$contactConversionV2ModuleName+"\"
$forPath = $forPath+$forServerName[0]
out-logfile -string $forPath
$networkLoggingDirectory+=$forPath
}
#At this time test for the paths and create them if not already there.
foreach ($directory in $networkLoggingDirectory)
{
out-logFile -string "Testing to see if network path already exists."
out-logfile -string $directory
[boolean]$forTest = test-path -path $directory
out-logfile -string ("Test Path Results: "+$forTest)
if ($forTest -eq $FALSE)
{
out-logfile -string "Directory does not exist -> create."
$loopCounter = 0
$stopLoop = $FALSE
do {
try{
New-Item -ItemType Directory -Force -Path $directory -errorAction STOP
$stopLoop = $TRUE
}
catch{
if ($loopCounter -gt 4)
{
out-logfile -string "Uanble to create the network directory."
out-logfile -string $_ -isError:$TRUE
}
else
{
out-logfile -string "Initial issue creating folder - sleep -> retry."
start-sleepProgress -sleepString "Initial issue creating folder - sleep -> retry." -sleepSeconds 5
$loopCounter = $loopCounter+1
}
}
} until ($stopLoop -eq $TRUE)
out-logfile -string "Testing creation of path..."
$forTest = test-path -path $directory
out-logfile -string ("Test Path Results: "+$forTest)
}
else
{
out-logfile -string "Network directory exists."
}
}
foreach ($directory in $networkLoggingDirectory)
{
out-logFile -string "Testing to see if network path already exists."
out-logfile -string $directory
$forDirectory = $directory+"\AuditData"
out-logfile -string $forDirectory
[boolean]$forTest = test-path -path $forDirectory
out-logfile -string ("Test Path Results: "+$forTest)
if ($forTest -eq $FALSE)
{
out-logfile -string "Directory does not exist -> create."
try{
New-Item -ItemType Directory -Force -Path $forDirectory -errorAction STOP
}
catch{
out-logfile -string "Uanble to create the network directory."
out-logfile -string $_ -isError:$TRUE
}
out-logfile -string "Testing creation of path..."
$forTest = test-path -path $forDirectory
out-logfile -string ("Test Path Results: "+$forTest)
}
else
{
out-logfile -string "Network directory exists."
}
}
#At this time we have validated the network directories exist.
#We have also pre-staged the audit data files.
#We will test now to see if the audit data folder exists in the directory for the controller specified.
#If so the items will be copied to the folder in the network share.
#This is necessary since the jobs provisioned utilze folders per machine and not the centralized folder.
$forDirectory = $logFolderPath+"\AuditData"
[boolean]$forTest=test-path -path $forDirectory
out-logfile -string "Testing for local audit data directory."
out-logfile -string ("Local Audit Data Directory: "+$forDirectory)
out-logfile -string ("Is directory present: "+$forTest)
if ($forTest -eq $TRUE)
{
out-logfile -string "Local audit data directory is present."
foreach ($directory in $networkLoggingDirectory)
{
$forDirectory=$logFolderPath+"\AuditData\*"
$forNetworkDirectory=$directory+"\AuditData\"
out-logfile -string ("Source Directory: "+$forDirectory)
out-logfile -string ("Target Directory: "+$forNetworkDirectory)
copy-item $forDirectory -destination $forNetworkDirectory -force
}
}
#The controller will split the addresses into contacts for each machine to process.
#To do this we simply take the total number of addresses divided by the number of controllers.
#The we create an array of address arrays.
out-logfile -string ("The number of addresses to process is = "+$totalAddressCount)
[int]$maxAddressesPerMachines = ($totalAddressCount / $servernames.count)
out-logfile -string ("The number of addresses per machine = "+$maxAddressesPerMachines)
[array]$contactSMTPAddressArray=@()
[array]$doArray=@()
[int]$forCounter = 0
#On the off chance someone tries do do a multi machine migration where the contact count is less than the server count.
#End the for loop when we've reached the count of contacts.
if ($totalAddressCount -lt $serverNames.count )
{
$forEnd = $totalAddressCount
}
else
{
$forEnd = $serverNames.count
}
for ($serverCounter = 0 ; $serverCounter -lt $forEnd ; $serverCounter++)
{
if ($serverCounter -eq ($forEnd - 1))
{
#This is the last array to be built - take all addresses that are left.
for ($maxCounter = $forCounter ; $maxCounter -lt $totalAddressCount ; $maxCounter++)
{
out-logfile -string ("For Counter = "+$maxCounter)
out-logfile -string ("SMTP Address Processed = "+$contactSMTPAddresses[$maxCounter])
$doArray+=$contactSMTPAddresses[$maxCounter]
}