AADConnect-CommunicationsTest.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
<#PSScriptInfo
 
.VERSION 4.1.1
 
.GUID 98fc7cbf-c135-4928-9cd7-499aea978c76
 
.DESCRIPTION AAD Communications Test: Use this script to test basic network connectivity to on-premises and online endpoints as well as name resolution.
 
.AUTHOR Aaron Guilmette
 
.COMPANYNAME Microsoft
 
.COPYRIGHT 2020
 
.TAGS Azure AzureAD Office365 AADConnect seamless sso prerequisites azure ad connect installation
 
.LICENSEURI
 
.PROJECTURI https://www.undocumented-features.com/2018/02/10/aad-connect-network-and-name-resolution-test/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>


<#
.SYNOPSIS
Test basic system, connectivity, and name resolution compatibility for AAD Connect.
 
Use this script to test basic network connectivity to on-premises and
online endpoints as well as name resolution.
 
If you are uncertain about your server's ability to connect to Office 365 for
the purposes of deploying Azure AD Connect or to local network resources for
configuring a multi-forest deployment, you can attempt to use this tool to report on
connectivity and name resolution success/failure. For more information, read the blog at
https://www.undocumented-features.com/2018/02/10/aad-connect-network-and-name-resolution-test/
or check out the entire history at https://www.undocumented-features.com/tag/aadnetwork/.
 
.PARAMETER ActiveDirectory
Run Active Directory checks (Single Label Domain, NetBIOS domain,
AD Recycle Bin enabled, Forest Functional Level)
 
.PARAMETER AllTests
Run all tests with default settings (ActiveDirectory, AzureADCredentialCheck, OnlineEndpoints
with Commercial endpoints, Network, Dns, SystemConfiguration). Can also set a Boolean FALSE
for parameters.
 
.PARAMETER AzureCredentialCheck
Check the specified credential for Azure AD suitability (valid password, is a
member of global administrators).
 
.PARAMETER DCs
Use this parameter to specify DCs to test against. Required if running on-
premises network or DNS tests. This is auto-populated from the LOGONSERVER
environment variable. If the server is not joined to a domain, populate this
attribute with a DC for the domain/forest you will be configuration AAD Connect
against.
 
.PARAMETER DebugLogging
Enable debug error logging to log file.
 
.PARAMETER Dns
Use this parameter to only run on-premises Dns tests. Requires FQDN and DCs
parameters to be specified.
 
.PARAMETER FixedDcRpcPort
Use this optional parameter to specify a fixed Rpc port for DC communications.
See https://support.microsoft.com/en-us/help/224196/restricting-active-directory-rpc-traffic-to-a-specific-port
for more information.
 
.PARAMETER InstallModules
Use this parameter to install modules used during tests, including MSOnline,
NuGet, PowerShell Gallery, Install-Module, and the Remote Server Administration
Tools.
 
.PARAMETER Logfile
Self-explanatory.
 
.PARAMETER Network
Use this parameter to only run local network tests. Requires FQDN and DCs parameters
to be specified if they are not automatically populated. They may not be automatically
populated if the server running this tool has not been joined to a domain. That is a
supported configuration; however, you will need to specify a forest FQDN and at least
one DC.
 
.PARAMETER OnlineEndPoints
Use this parameter to conduct communication tests against online endpoints.
 
.PARAMETER OnlineEndPointTarget
Use this optional parameter to select GCC, Commercial, DOD, or GCC High environments.
 
.PARAMETER OptionalADPortTest
Use this optional parameter to specify ports that you may not need for communications.
While the public documentation says port 88 is required for Kerberos, it may not be used
in certain circumstances (such as adding an AD connector to a remote forest after AAD
connect has been intalled). Optional ports include:
- 88 (Kerberos)
- 636 (Secure LDAP)
- 3269 (Secure Global Catalog)
 
You can enable secure LDAP after the AAD Connect installation has completed.
 
.PARAMETER SkipDcDnsPortCheck
If you are not using DNS services provided by the AD Site / Logon DC, then you may want
to skip checking port 53. You must still be able to resolve ._ldap._tcp.<forestfqdn>
in order for the Active Directory Connector configuration to succeed.
 
.PARAMETER SystemConfiguration
Report on system configuration items, including installed Windows Features, TLS
registry entries and proxy configurations.
 
.EXAMPLE
.\AADConnect-CommunicationsTest.ps1
Runs all tests and writes to default log file location (YYYY-MM-DD_AADConnectivity.txt)
 
.EXAMPLE
.\AADConnect-CommunicationsTest.ps1 -Dns -Network
Runs Dns and Network tests and writes to default log file location (YYYY-MM-DD_AADConnectivity.txt).
 
.EXAMPLE
.\AADConnect-CommunicationsTest.ps1 -OnlineEndPoints -OnlineEndPointTarget DOD
Runs OnlineEndPoints test using the U.S. Department of Defense online endpoints list
and writes to default log file location (YYYY-MM-DD_AADConnectivity.txt).
 
.EXAMPLE
.\AADConnect-CommunicationsTest.ps1 -AzureCredentialCheck -Network -DCs dc1.contoso.com -ForestFQDN contoso.com
Runs Azure Credential Check and local networking tests using DC dc1.contoso.com and
the forest contoso.com and writes to the default log file location
(YYYY-MM-DD_AADConnectivity.txt).
 
.EXAMPLE
.\AADConnect-CommunicationsTest.ps1 -AllTests -Network:$false
Run All system tests using defaults, excluding Network tests.
 
.LINK
https://www.undocumented-features.com/2018/02/10/aad-connect-network-and-name-resolution-test/
 
.LINK
https://aka.ms/aadnetwork
 
.NOTES
- 2020-04-29 Updated DOD/GCCH endpoints.
                Added check for RODCs in current site.
- 2020-04-03 Rebranded as v4 and uploaded to PowerShellGallery.com
- 2020-01-28 Updated endpoints.
                Updated to retrieve Azure AD Tenant ID for checking <tenantid>.registration.appproxy.net.
                Added Windows2016Forest forest mode to system configuration function.
                Cleaned up some error handling.
- 2019-05-07 Updated to skip ActiveDirectory checks if ForestFQDN and DCs values are empty and can't be calculated.
                Updated logging for desktop/client versions in System Configuration.
- 2019-03-13 Resolved issue with Azure credential check not running automatically.
                Resolved issue with Azure credential not displaying the user identity.
                Resolved issue with 'optional' network services testing in OnlineEndPoints test.
- 2019-03-12 Restructured how parameters are processed using PSBoundParameters.
                Removed SkipAzureADCredentialCheck parameter.
                Added additional error trapping around Resolve-DnsName.
                Refreshed endpoints for AAD.
                Added AllTests parameter. AllTests also supports setting specific tests to Boolean FALSE.
- 2018-10-28 Updated InstallModules param checking.
- 2018-10-24 Initial release checking RSOP data for PowerShell Transcription GPO.
- 2018-09-14 Added check for installation edition (server, server core, client, nano) based on https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-operatingsystem.
- 2018-08-30 Added -InstallModules switch.
- 2018-07-30 Removed single-label domain references.
- 2018-07-23 Added supported AD forest modes in System Configruation.
                Added AD Recycle Bin configuration check.
                Added ActiveDirectory parameter.
- 2018-07-17 Updated Commerical/GCC endpoints:
                $CRL += http://ocsp.msocsp.com
                $RequiredResources += adminwebservice-s1-co2.microsoftonline.com
                $RequiredResourcesEndpoints += https://adminwebservice-s1-co2.microsoftonline.com/provisioningwebservice.svc
- 2018-06-15 Fixed Windows 2016 detection display issue.
                 Fixed issue querying PowerShell Transcription.
- 2018-06-14 Updated query for system.net/defaultproxy/proxy.
                Added reg key for Wow6432Node/SchUseStrongCrypto for TLS 1.2.
                Added OS-specific support for determining .NET versions.
                Added OS-specific registry keys to test for TLS 1.2 configuration.
- 2018-04-05 Removed proxy.cloudwebappproxy.net from Seamless SSO endpoint test.
- 2018-04-03 Updated endpoints for Seamless SSO.
- 2018-04-03 Added endpoints management.core.windows.net, s1.adhybridhealth.azure.com
- 2018-04-02 Added endpoints for Seamless SSO.
- 2018-02-16 Added optional port for Secure Global Catalog (3269)
- 2018-02-14 Added FixedDcRpcPort, OptionalADPortTest, SystemConfiguration parameters
- 2018-02-14 Added test for servicebus.windows.net to online endpoints
- 2018-02-14 Expanded system configuration tests to capture TLS 1.2 configuration
- 2018-02-14 Expanded system configuration tests to capture required server features
- 2018-02-13 Added OnlineEndPointTarget parameter for selecting Commercial, GCC, DOD, or GCC high.
- 2018-02-13 Added proxy config checks.
- 2018-02-12 Added additional CRL/OCSP endpoints for Entrust and Verisign.
- 2018-02-12 Added additional https:// test endpoints.
- 2018-02-12 Added DebugLogging parameter and debug logging data.
- 2018-02-12 Added extended checks for online endpoints.
- 2018-02-12 Added check for Azure AD credential (valid/invalid password, is Global Admin)
- 2018-02-12 Updated parameter check when running new mixes of options.
- 2018-02-11 Added default values for ForestFQDN and DCs.
- 2018-02-11 Added SkipDcDnsPortCheck parameter.
- 2018-02-10 Resolved issue where tests would run twice under some conditions.
- 2018-02-09 Initial release.
#>


param (
    [switch]$ActiveDirectory,
    [switch]$AllTests,
    [switch]$AzureCredentialCheck,
    [Parameter(HelpMessage="Specify the azure credential to check in the form of user@domain.com or user@tenant.onmicrosoft.com")]$Credential,
    [array]$DCs, 
    [switch]$DebugLogging,
    [switch]$Dns,
    [int]$FixedDcRpcPort,
    [string]$ForestFQDN,
    [switch]$InstallModules,
    [string]$Logfile = (Get-Date -Format yyyy-MM-dd) + "_AADConnectConnectivity.txt",
    [switch]$Network,
    [switch]$OnlineEndPoints,
    [ValidateSet("Commercial","DOD","GCC","GCCHigh")]
    [string]$OnlineEndPointTarget = "Commercial",
    [switch]$OptionalADPortTest,
    [ValidateSet("PasswordWriteBack")][string[]]$OptionalFeatureCheck,
    [switch]$SkipDcDnsPortCheck,
    [switch]$SystemConfiguration
)

$Version = "4.0"
# Hide the test-netconnection progress meter
$ExistingProgressPreference = $ProgressPreference
$global:ProgressPreference = 'SilentlyContinue'

# If no DCs are supplied and no LOGON server value is available, then set
# $ActiveDirectory to $false and exclude ActiveDirectory from executed tests
If (!$DCs)
{
    If ($env:LOGONSERVER -and $env:USERDNSDOMAIN)
    {
        $DCs = (Get-ChildItem Env:\Logonserver).Value.ToString().Trim("\") + "." + (Get-ChildItem Env:\USERDNSDOMAIN).Value.ToString()
    }
    Else
    {
        $ActiveDirectory = $false
    }
}

# If no ForestFQDN is supplied, check for it. If UserDnsDomain isn't populated,
# set $ActiveDirectory to $false and exclude ActiveDirectory from executed tasks
If (!$ForestFQDN)
{
    If ($env:USERDNSDOMAIN)
    {
        $ForestFQDN = (Get-ChildItem Env:\USERDNSDOMAIN).Value.ToString()
    }
    Else
    {
        $ActiveDirectory = $false
    }
}

## Functions
# Logging function
function Write-Log([string[]]$Message, [string]$LogFile = $Script:LogFile, [switch]$ConsoleOutput, [ValidateSet("SUCCESS", "INFO", "WARN", "ERROR", "DEBUG")][string]$LogLevel)
{
    $Message = $Message + $Input
    If (!$LogLevel) { $LogLevel = "INFO" }
    switch ($LogLevel)
    {
        SUCCESS { $Color = "Green" }
        INFO { $Color = "White" }
        WARN { $Color = "Yellow" }
        ERROR { $Color = "Red" }
        DEBUG { $Color = "Gray" }
    }
    if ($Message -ne $null -and $Message.Length -gt 0)
    {
        $TimeStamp = [System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss")
        if ($LogFile -ne $null -and $LogFile -ne [System.String]::Empty)
        {
            Out-File -Append -FilePath $LogFile -InputObject "[$TimeStamp] [$LogLevel] $Message"
        }
        if ($ConsoleOutput -eq $true)
        {
            Write-Host "[$TimeStamp] [$LogLevel] :: $Message" -ForegroundColor $Color
        }
    }
}

# Test Office 365 Credentials
function AzureCredential
{
    If ($SkipAzureCredentialCheck) { "Skipping Azure AD Credential Check due to parameter.";  Continue}
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting Office 365 global administrator and credential tests."
    If (!$Credential)
    {
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Credential required to validate Office 365 credentials. Enter global admin credential."
        $Credential = Get-Credential -Message "Office 365 Global Administrator"
    }
    # Attempt MSOnline installation
    Try
    {
        MSOnline
    }
    Catch
    {
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to proceed with MSOnline check. Please install the Microsoft Online Services Module separately and re-run the script." -ConsoleOutput
        
    }
    
    # Attempt to log on as user
    try
    {
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Attempting logon as $($Credential.UserName) to Azure Active Directory."
        $LogonResult = Connect-MsolService -Credential $Credential -ErrorAction Stop
        If ($LogonResult -eq $null)
        {
            Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Successfully logged on to Azure Active Directory as $($Credential.UserName)." -ConsoleOutput
            ## Attempt to check membership in Global Admins, which is labelled as "Company Administrator" in the tenant
            $RoleId = (Get-MsolRole -RoleName "Company Administrator").ObjectId
            If ((Get-MsolRoleMember -RoleObjectId $RoleId).EmailAddress -match "\b$($Credential.UserName)")
            {
                Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "User $($Credential.Username) is a member of Global Administrators." -ConsoleOutput
            }
            Else
            {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "User $($Credential.UserName) is not a member of Global Administrators. In order for Azure Active Directory synchronization to be successful, the user must have the Global Administrators role granted in Office 365. Grant the appropriate access or select another user account to test."        
            }
        }
        Else
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to verify logon to Azure Active Directory as $($Credential.UserName)." -ConsoleOutput        
        }
    }
    catch
    {
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to log on to Azure Active Directory as $($Credential.UserName). Check $($Logfile) for additional details." -ConsoleOutput
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
    }
    
    # Attempt to get Tenant ID
    If (Get-Module AzureAD -ListAvailable -ea silentlycontinue)
    { try
        {
            Import-Module AzureAD
            $AADLogonResult = Connect-AzureAD $Credential -ea Stop
            if ($AADLogonResult.TenantID) { $TenantID = "$($AADLogonResult.TenantId).registration.msappproxy.net" }
        }
        catch
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Exception while attempting to log onto Azure AD. Exception data:"
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
        }
    }
} # End Function AzureCredential

# Test for/install MSOnline components
function MSOnline
{
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Checking Microsoft Online Services Module."
    If (!(Get-Module -ListAvailable MSOnline -ea silentlycontinue) -and $InstallModules)
    {
        # Check if Elevated
        $wid = [system.security.principal.windowsidentity]::GetCurrent()
        $prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
        $adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
        if ($prp.IsInRole($adm))
        {
            Write-Log -LogFile $Logfile -LogLevel SUCCESS -ConsoleOutput -Message "Elevated PowerShell session detected. Continuing."
        }
        else
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -ConsoleOutput -Message "This application/script must be run in an elevated PowerShell window. Please launch an elevated session and try again."
            Break
        }
        
        Write-Log -LogFile $Logfile -LogLevel INFO -ConsoleOutput -Message "This requires the Microsoft Online Services Module. Attempting to download and install."
        wget https://download.microsoft.com/download/5/0/1/5017D39B-8E29-48C8-91A8-8D0E4968E6D4/en/msoidcli_64.msi -OutFile $env:TEMP\msoidcli_64.msi
        If (!(Get-Command Install-Module))
        {
            wget https://download.microsoft.com/download/C/4/1/C41378D4-7F41-4BBE-9D0D-0E4F98585C61/PackageManagement_x64.msi -OutFile $env:TEMP\PackageManagement_x64.msi
        }
        If ($DebugLogging) { Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Installing Sign-On Assistant." }
        msiexec /i $env:TEMP\msoidcli_64.msi /quiet /passive
        If ($DebugLogging) { Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Installing PowerShell Get Supporting Libraries." }
        msiexec /i $env:TEMP\PackageManagement_x64.msi /qn
        If ($DebugLogging) { Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Installing PowerShell Get Supporting Libraries (NuGet)." }
        Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force -Confirm:$false
        If ($DebugLogging) { Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Installing Microsoft Online Services Module." }
        Install-Module MSOnline -Confirm:$false -Force
        If (!(Get-Module -ListAvailable MSOnline))
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -ConsoleOutput -Message "This Configuration requires the MSOnline Module. Please download from https://connect.microsoft.com/site1164/Downloads/DownloadDetails.aspx?DownloadID=59185 and try again."
            
            Break
        }
    }
    If (Get-Module -ListAvailable MSOnline) { Import-Module MSOnline -Force }
    Else { Write-Log -LogFile $Logfile -LogLevel ERROR -ConsoleOutput -Message "This Configuration requires the MSOnline Module. Please download from https://connect.microsoft.com/site1164/Downloads/DownloadDetails.aspx?DownloadID=59185 and try again." }
    
    # Check for Azure AD Module
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Checking for Microsoft Azure AD Module."
    if (!($Result = Get-Module -ListAvailable AzureAD))
    {
        try
        {
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "Attempting to install Azure AD Module."
            Install-Module AzureAD -Force -SkipPublisherCheck -ea stop
        }
        catch { Write-Log -LogFile $Logfile -Message $_.Exception.Message.ToString() -LogLevel ERROR }
    }
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished Microsoft Online Service Module check."
} # End Function MSOnline

# Test Online Networking Only
function OnlineEndPoints
{
    switch -regex ($OnlineEndPointTarget)
    {
        'commercial|gcc'
        {
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting Online Endpoints tests (Commercial/GCC)."
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "See https://support.office.com/en-us/article/office-365-urls-and-ip-address-ranges-8548a211-3fe7-47cb-abb1-355ea5aa88a2"
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "for more details on Commercial/GCC endpoints."
            $CRL = @(
                "http://ocsp.msocsp.com",
                "http://crl.microsoft.com/pki/crl/products/microsoftrootcert.crl",
                "http://mscrl.microsoft.com/pki/mscorp/crl/msitwww2.crl",
                "http://ocsp.verisign.com",
                "http://ocsp.entrust.net")
            $RequiredResources = @(
                "adminwebservice.microsoftonline.com",
                #"adminwebservice-s1-co2.microsoftonline.com", # Removed 2020-01-28
                "login.microsoftonline.com",
                "provisioningapi.microsoftonline.com",
                "login.windows.net",
                "secure.aadcdn.microsoftonline-p.com",
                "management.core.windows.net",
                #"bba800-anchor.microsoftonline.com", # Removed 2020-01-28
                "graph.windows.net",
                "aadcdn.msauth.net",
                "aadcdn.msftauth.net",
                "ccscdn.msauth.net",
                "ccscdn.msftauth.net",
                "becws.microsoftonline.com", # added 2020-01-28
                "api.passwordreset.microsoftonline.com" # Self-service Password Reset, added 2020-01-28
                )
            $RequiredResourcesEndpoints = @(
                "https://adminwebservice.microsoftonline.com/provisioningservice.svc",
                # "https://adminwebservice-s1-co2.microsoftonline.com/provisioningservice.svc", # Removed 2020-01-28
                "https://login.microsoftonline.com",
                "https://provisioningapi.microsoftonline.com/provisioningwebservice.svc",
                "https://login.windows.net",
                "https://secure.aadcdn.microsoftonline-p.com/ests/2.1.5975.9/content/cdnbundles/jquery.1.11.min.js"
                )
            $OptionalResources = @(
                "management.azure.com",
                "policykeyservice.dc.ad.msft.net",
                "s1.adhybridhealth.azure.com",
                "autoupdate.msappproxy.net",
                "adds.aadconnecthealth.azure.com",
                "account.activedirectory.windowsazure.com", # myapps portal, added 2020-01-28
                "enterpriseregistration.windows.net", # device registration
                "clientconfig.microsoftonline-p.net" #added 2020-01-28
                )
            $OptionalResourcesEndpoints = @(
                "https://policykeyservice.dc.ad.msft.net/clientregistrationmanager.svc",
                "https://device.login.microsoftonline.com" # Hybrid device registration
            )
            $SeamlessSSOEndpoints = @(
                "autologon.microsoftazuread-sso.com",
                "aadg.windows.net.nsatc.net",
                "0.register.msappproxy.net",
                "0.registration.msappproxy.net",
                "proxy.cloudwebappproxy.net"
            )
            # Tenant Registration Endpoint
            If ($TenantID) { $SeamlessSSOEndpoints += $Tenant } # Added 2020-01-28
            # Use the AdditionalResources array to specify items that need a port test on a port other
            # than 80 or 443.
            $AdditionalResources = @(
                # "watchdog.servicebus.windows.net:5671" # Removed 2020-01-28
                )
        }
        
        'dod|gcchigh'
        {
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting Online Endpoints tests (DOD)."
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "See https://support.office.com/en-us/article/office-365-u-s-government-dod-endpoints-5d7dce60-4892-4b58-b45e-ee42fe8a907f"
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "for more details on DOD/GCCHigh endpoints."
            $CRL = @(
                "http://ocsp.msocsp.com",
                "https://mscrl.microsoft.com/pki/mscorp/crl/msitwww2.crl",
                "http://crl.microsoft.com/pki/crl/products/microsoftrootcert.crl",
                "http://ocsp.verisign.com",
                "http://ocsp.entrust.net")
            $RequiredResources = @(
                "adminwebservice.gov.us.microsoftonline.com",
                "adminwebservice-s1-bn1a.microsoftonline.com",
                "becws.gov.us.microsoftonline.com," # added 2020-01-28
                "dod-graph.microsoft.us", # added 2020-01-28
                "adminwebservice-s1-dm2a.microsoftonline.com",
                "graph.microsoftazure.us", # added 2020-01-28
                "login.microsoftonline.us",
                "login.microsoftonline.com",
                "login.microsoftonline-p.com",
                "loginex.microsoftonline.com",
                "login-us.microsoftonline.com",
                "login.windows.net",
                "graph.windows.net",
                "aadcdn.msauth.net", # have not verified
                "aadcdn.msftauth.net", # have not verified
                "ccscdn.msauth.net", # have not verified
                "ccscdn.msftauth.net", # have not verified
                "provisioningapi.gov.us.microsoftonline.com",
                "provisioningapi-s1-dm2a.microsoftonline.com",
                "provisioningapi-s1-dm2r.microsoftonline.com",
                "secure.aadcdn.microsoftonline-p.com",
                "clientconfig.microsoftonline-p.net" # added 2020-01-28
            )
            $RequiredResourcesEndpoints = @(
                "https://adminwebservice.gov.us.microsoftonline.com/provisioningservice.svc",
                "https://adminwebservice-s1-bn1a.microsoftonline.com/provisioningservice.svc",
                "https://adminwebservice-s1-dm2a.microsoftonline.com/provisioningservice.svc",
                "https://login.microsoftonline.us"
                "https://login.microsoftonline.com",
                "https://loginex.microsoftonline.com",
                "https://login-us.microsoftonline.com",
                "https://login.windows.net",
                "https://provisioningapi.gov.us.microsoftonline.com/provisioningwebservice.svc",
                "https://provisioningapi-s1-dm2a.microsoftonline.com/provisioningwebservice.svc",
                "https://provisioningapi-s1-dm2r.microsoftonline.com/provisioningwebservice.svc"
                "https://secure.aadcdn.microsoftonline-p.com/ests/2.1.5975.9/content/cdnbundles/jquery.1.11.min.js")
            # These optional endpoints are newly listed for DOD/GCCHigh
            $OptionalResources = @(
                "management.azure.com", 
                "policykeyservice.aadcdi.azure.us" # Azure AD Connect Health
                # ,"enterpriseregistration.windows.net" # Not currently listed for DOD/GCCH
            )
            
            $OptionalResourcesEndpoints = @(
                "https://policykeyservice.aadcdi.azure.us/clientregistrationmanager.svc" # Azure AD Connect Health
                # ,"https://enterpriseregistration.windows.net" # Not currently listed for DOD/GCCH
            )
            # Use the AdditionalResources array to specify items that need a port test on a port other
            # than 80 or 443.
            $AdditionalResources = @(
                # "watchdog.servicebus.windows.net:5671" # ServiceBus endpoints no longer needed
                )
            #>
        }
    }
    
    # CRL Endpoint tests
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing CRL endpoint tests (Invoke-WebRequest)." -ConsoleOutput
    foreach ($url in $CRL)
    {
        try
        {
            $Result = Invoke-WebRequest -Uri $url -ea stop -wa silentlycontinue
            Switch ($Result.StatusCode)
            {
                200 { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Successfully obtained CRL from $($url)." -ConsoleOutput }
                400 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Bad request." -ConsoleOutput;  }
                401 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Unauthorized." -ConsoleOutput;  }
                403 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Forbidden." -ConsoleOutput;  }
                404 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Not found." -ConsoleOutput;  }
                407 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Proxy authentication required." -ConsoleOutput;  }
                502 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Bad gateway (likely proxy)." -ConsoleOutput;  }
                503 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Service unavailable (transient, try again)." -ConsoleOutput;  }
                504 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to obtain CRL from $($url): Gateway timeout (likely proxy)." -ConsoleOutput;  }
                default
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to obtain CRL from $($url)" -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($Result)"            
                }
            }
        }
        catch
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Exception: Unable to obtain CRL from $($url)" -ConsoleOutput
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
        }
        finally
        {
            If ($DebugLogging)
            {
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($url)."
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $Result.StatusCode
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $Result.StatusDescription
                If ($Result.RawContent.Length -lt 400)
                {
                    $DebugContent = $Result.RawContent -join ";"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DebugContent
                }
                Else
                {
                    $DebugContent = $Result.RawContent.Substring(0, 400) -join ";"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DebugContent
                }
            }
        }
    } # End Foreach CRL
        
    # Required Resource tests
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing Required Resources (TCP:443)." -ConsoleOutput
    foreach ($url in $RequiredResources)
    {
        try { [array]$ResourceAddresses = (Resolve-DnsName $url -ErrorAction stop).IP4Address }
        catch { $ErrorMessage = $_;  Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to resolve host $($url)." -ConsoleOutput; Write-Log -LogFile $Logfile -LogLevel ERROR -Message $($ErrorMessage); Continue }
        foreach ($ip4 in $ResourceAddresses)
        {
            try
            {
                $Result = Test-NetConnection $ip4 -Port 443 -ea stop -wa silentlycontinue
                switch ($Result.TcpTestSucceeded)
                {
                    true { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "TCP connection to $($url) [$($ip4)]:443 successful." -ConsoleOutput }
                    false { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "TCP connection to $($url) [$($ip4)]:443 failed." -ConsoleOutput;  }
                }
            }
            catch
            {
                Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Error resolving or connecting to $($url) [$($ip4)]:443" -ConsoleOutput
                Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
            }
            finally
            {
                If ($DebugLogging)
                {
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($url) [$($Result.RemoteAddress)]:443."
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote endpoint: $($url)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote port: $($Result.RemotePort)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Interface Alias: $($Result.InterfaceAlias)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Source Interface Address: $($Result.SourceAddress.IPAddress)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Succeeded: $($Result.PingSucceeded)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) Status: $($Result.PingReplyDetails.Status)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) RoundTripTime: $($Result.PingReplyDetails.RoundtripTime)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "TCPTestSucceeded: $($Result.TcpTestSucceeded)"
                }
            }
        } 
    } # End Foreach Resources
        
    # Option Resources tests
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing Optional Resources (TCP:443)." -ConsoleOutput
    foreach ($url in $OptionalResources)
    {
        try { [array]$ResourceAddresses = (Resolve-DnsName $url -ErrorAction stop).IP4Address }
        catch { $ErrorMessage = $_; Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to resolve host $($url)." -ConsoleOutput; Write-Log -LogFile $Logfile -LogLevel ERROR -Message $($ErrorMessage); Continue }
        
        foreach ($ip4 in $ResourceAddresses)
        {
            try
            {
                $Result = Test-NetConnection $ip4 -Port 443 -ea stop -wa silentlycontinue
                switch ($Result.TcpTestSucceeded)
                {
                    true { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "TCP connection to $($url) [$($ip4)]:443 successful." -ConsoleOutput }
                    false {
                        Write-Log -LogFile $Logfile -LogLevel WARN -Message "TCP connection to $($url) [$($ip4)]:443 failed." -ConsoleOutput; 
                        If ($DebugLogging) { Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $($Result) }
                    }
                }
            }
            catch
            {
                Write-Log -LogFile $Logfile -LogLevel WARN -Message "Error resolving or connecting to $($url) [$($ip4)]:443" -ConsoleOutput
                Write-Log -LogFile $Logfile -LogLevel WARN -Message "$($_.Exception.Message)"
            }
            finally
            {
                If ($DebugLogging)
                {
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($url) [$($Result.RemoteAddress)]:443."
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote endpoint: $($url)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote port: $($Result.RemotePort)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Interface Alias: $($Result.InterfaceAlias)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Source Interface Address: $($Result.SourceAddress.IPAddress)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Succeeded: $($Result.PingSucceeded)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) Status: $($Result.PingReplyDetails.Status)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) RoundTripTime: $($Result.PingReplyDetails.RoundtripTime)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "TCPTestSucceeded: $($Result.TcpTestSucceeded)"
                }
            }
        }
    } # End Foreach OptionalResources
        
    # Required Resources Endpoints tests
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing Required Resources Endpoints (Invoke-Webrequest)." -ConsoleOutput
    foreach ($url in $RequiredResourcesEndpoints)
    {
        try
        {
            $Result = Invoke-WebRequest -Uri $url -ea stop -wa silentlycontinue
            Switch ($Result.StatusCode)
            {
                200 { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Successfully connected to $($url)." -ConsoleOutput }
                400 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Bad request." -ConsoleOutput;  }
                401 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Unauthorized." -ConsoleOutput;  }
                403 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Forbidden." -ConsoleOutput;  }
                404 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Not found." -ConsoleOutput;  }
                407 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Proxy authentication required." -ConsoleOutput;  }
                502 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Bad gateway (likely proxy)." -ConsoleOutput;  }
                503 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Service unavailable (transient, try again)." -ConsoleOutput;  }
                504 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Gateway timeout (likely proxy)." -ConsoleOutput;  }
                default
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "OTHER: Failed to contact $($url)" -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($Result)" -ConsoleOutput            
                }
            }
        }
        catch
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Exception: Unable to contact $($url)" -ConsoleOutput
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
        }
        finally
        {
            If ($DebugLogging)
            {
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($url)."
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $Result.StatusCode
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $Result.StatusDescription
                If ($Result.RawContent.Length -lt 400)
                {
                    $DebugContent = $Result.RawContent -join ";"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DebugContent
                }
                Else
                {
                    $DebugContent = $Result.RawContent -join ";"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DebugContent.Substring(0, 400)
                }
            }
        }
    } # End Foreach RequiredResourcesEndpoints
    
    # Optional Resources Endpoints tests
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing Optional Resources Endpoints (Invoke-Webrequest)." -ConsoleOutput
    foreach ($url in $OptionalResourcesEndpoints)
    {
        try
        {
            $Result = Invoke-WebRequest -Uri $url -ea stop -wa silentlycontinue
            Switch ($Result.StatusCode)
            {
                200 { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Successfully connected to $($url)." -ConsoleOutput }
                400 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Bad request." -ConsoleOutput;  }
                401 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Unauthorized." -ConsoleOutput;  }
                403 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Forbidden." -ConsoleOutput;  }
                404 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Not found." -ConsoleOutput;  }
                407 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Proxy authentication required." -ConsoleOutput;  }
                502 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Bad gateway (likely proxy)." -ConsoleOutput;  }
                503 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Service unavailable (transient, try again)." -ConsoleOutput;  }
                504 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Failed to contact $($url): Gateway timeout (likely proxy)." -ConsoleOutput;  }
                default
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "OTHER: Failed to contact $($url)" -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($Result)" -ConsoleOutput
                }
            }
        }
        catch
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Exception: Unable to contact $($url)" -ConsoleOutput
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
        }
        finally
        {
            If ($DebugLogging)
            {
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($url)."
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $Result.StatusCode
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $Result.StatusDescription
                If ($Result.RawContent.Length -lt 400)
                {
                    $DebugContent = $Result.RawContent -join ";"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DebugContent
                }
                Else
                {
                    $DebugContent = $Result.RawContent -join ";"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DebugContent.Substring(0, 400)
                }
            }
        }
    } # End Foreach RequiredResourcesEndpoints
        
    # Seamless SSO Endpoints
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing Seamless SSO Endpoints (TCP:443)." -ConsoleOutput
    foreach ($url in $SeamlessSSOEndpoints)
    {
        try
        {
            [array]$ResourceAddresses = (Resolve-DnsName $url -ErrorAction stop).IP4Address
        }
        catch
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to resolve host $($url)." -ConsoleOutput
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
            Continue
        }
        
        foreach ($ip4 in $ResourceAddresses)
        {
            try
            {
                $Result = Test-NetConnection $ip4 -Port 443 -ea stop -wa silentlycontinue
                switch ($Result.TcpTestSucceeded)
                {
                    true { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "TCP connection to $($url) [$($ip4)]:443 successful." -ConsoleOutput }
                    false { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "TCP connection to $($url) [$($ip4)]:443 failed." -ConsoleOutput;  }
                }
            }
            catch
            {
                Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Error resolving or connecting to $($url) [$($ip4)]:443" -ConsoleOutput
                Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
                
            }
            finally
            {
                If ($DebugLogging)
                {
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($url) [$($Result.RemoteAddress)]:443."
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote endpoint: $($url)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote port: $($Result.RemotePort)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Interface Alias: $($Result.InterfaceAlias)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Source Interface Address: $($Result.SourceAddress.IPAddress)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Succeeded: $($Result.PingSucceeded)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) Status: $($Result.PingReplyDetails.Status)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) RoundTripTime: $($Result.PingReplyDetails.RoundtripTime)"
                    Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "TCPTestSucceeded: $($Result.TcpTestSucceeded)"
                }
            }
        }
    } # End Foreach Resources
        
    # Additional Resources tests
    If ($AdditionalResources)
    {
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Testing Additional Resources Endpoints (Invoke-Webrequest)." -ConsoleOutput
        foreach ($url in $AdditionalResources)
        {
            if ($url -match "\:")
            {
                $Name = $url.Split(":")[0]
                try { [array]$Resources = (Resolve-DnsName $Name -ErrorAction stop).IP4Address }
                catch
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to resolve host $($Name)." -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
                    Continue }
                
                #[array]$Resources = (Resolve-DnsName $Name).Ip4Address
                $ResourcesPort = $url.Split(":")[1]
            }
            Else
            {
                $Name = $url
                try
                {
                    [array]$Resources = (Resolve-DnsName $Name -ErrorAction stop).IP4Address
                }
                catch
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to resolve host $($url)." -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
                    Continue
                }
                
                #[array]$Resources = (Resolve-DnsName $Name).IP4Address
                $ResourcesPort = "443"
            }
            foreach ($ip4 in $Resources)
            {
                try
                {
                    $Result = Test-NetConnection $ip4 -Port $ResourcesPort -ea stop -wa silentlycontinue
                    switch ($Result.TcpTestSucceeded)
                    {
                        true { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "TCP connection to $($Name) [$($ip4)]:$($ResourcesPort) successful." -ConsoleOutput }
                        false {
                            Write-Log -LogFile $Logfile -LogLevel WARN -Message "TCP connection to $($Name) [$($ip4)]:$($ResourcesPort) failed." -ConsoleOutput
                            
                            If ($DebugLogging) { Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $($Result) }
                        }
                    }
                }
                catch
                {
                    Write-Log -LogFile $Logfile -LogLevel WARN -Message "Error resolving or connecting to $($Name) [$($ip4)]:$($ResourcesPort)" -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel WARN -Message "$($_.Exception.Message)"
                    
                }
                finally
                {
                    If ($DebugLogging)
                    {
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($Name) [$($Result.RemoteAddress)]:443."
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote endpoint: $($Name)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote port: $($Result.RemotePort)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Interface Alias: $($Result.InterfaceAlias)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Source Interface Address: $($Result.SourceAddress.IPAddress)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Succeeded: $($Result.PingSucceeded)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) Status: $($Result.PingReplyDetails.Status)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) RoundTripTime: $($Result.PingReplyDetails.RoundtripTime)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "TCPTestSucceeded: $($Result.TcpTestSucceeded)"
                    }
                }
            } # End ForEach ip4
        } # End ForEach AdditionalResources
    } # End IF AdditionalResources
    
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished Online Endpoints tests."
} # End Function OnlineEndPoints

# Test for Optional Feature availability
function OptionalFeatureCheck
{
    If (!$Credential)
    {
        $Credential = Get-Credential -Message "Office 365 Global Administrator account"
    }
    
    If (!(Get-Module MSOnline)) { Import-Module MSOnline }
    else
    {
        try { $Result = Get-Command Get-MsolAccountSku -ea Stop }
        catch { Write-Log -LogFile ERROR -ConsoleOutput -LogLevel ERROR -Message "$($_.Exception.Message)" }
    }
    If (!($Result)) { Connect-MsolService -Credential $Credential }
    
    switch ($OptionalFeatureCheck)
    {
        "PasswordWriteBack" {
            [array]$Skus = Get-MsolAccountSku
            [array]$SkusWithAADPremiumServicePlan = @()
            foreach ($Sku in $Skus)
            {
                if ($Sku.ServiceStatus.ServicePlan.ServiceName -match "AAD_PREMIUM") { $SkusWithAADPremiumServicePlan += $Sku.SkuPartNumber }
            }
            If ($SkusWithAADPremiumServicePlan)
            {
                $SkusWithAADPremiumServicePlan | % { Write-Log -LogFile $Logfile -LogLevel INFO -Message "$($_) contains an Azure AD Premium Service to enable Password Write Back."}
            }
        }
    }
} # End function OptionalFeatureCheck

# Test Local Networking Only
function Network
{
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting local network port tests." -ConsoleOutput
    If (!$DCs)
    {
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "If testing on-premises networking, you must specify at least one on-premises domain controller." -ConsoleOutput
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "You can skip this test in the future with the parameter: -Network:`$false" -ConsoleOutput
    }
    
    Else
    {
        Foreach ($Destination in $DCs)
        {
            foreach ($Port in $Ports)
            {
                Try
                {
                    $Result = (Test-NetConnection -ComputerName $Destination -Port $Port -ea Stop -wa SilentlyContinue)
                    Switch ($Result.TcpTestSucceeded)
                    {
                        True
                        {
                            Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "TCP connection to $($Destination):$($Port) succeeded." -ConsoleOutput
                        }
                        False
                        {
                            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "TCP connection to $($Destination):$($Port) failed." -ConsoleOutput
                            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$Result"
                        }
                    } # End Switch
                }
                Catch
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Exception: Error attempting TCP connection to $($Destination):$($Port)." -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
                }
                Finally
                {
                    If ($DebugLogging)
                    {
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($Destination) [$($Result.RemoteAddress)]:$($Port)."
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote endpoint: $($Destination)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote port: $($Result.RemotePort)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Interface Alias: $($Result.InterfaceAlias)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Source Interface Address: $($Result.SourceAddress.IPAddress)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Succeeded: $($Result.PingSucceeded)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) Status: $($Result.PingReplyDetails.Status)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) RoundTripTime: $($Result.PingReplyDetails.RoundtripTime)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "TCPTestSucceeded: $($Result.TcpTestSucceeded)"
                    }
                }
            } # End Foreach Port in Ports
            foreach ($Port in $OptionalADPorts)
            {
                Try
                {
                    $Result = (Test-NetConnection -ComputerName $Destination -Port $Port -ea Stop -wa SilentlyContinue)
                    Switch ($Result.TcpTestSucceeded)
                    {
                        True
                        {
                            Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "TCP connection to $($Destination):$($Port) succeeded." -ConsoleOutput
                        }
                        False
                        {
                            Write-Log -LogFile $Logfile -LogLevel WARN -Message "TCP connection to $($Destination):$($Port) failed." -ConsoleOutput
                            Write-Log -LogFile $Logfile -LogLevel WARN -Message "$Result"
                        }
                    } # End Switch
                }
                Catch
                {
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Error attempting TCP connection to $($Destination):$($Port)." -ConsoleOutput
                    Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"    
                }
                Finally
                {
                    If ($DebugLogging)
                    {
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($Destination) [$($Result.RemoteAddress)]:$($Port)."
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote endpoint: $($Destination)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Remote port: $($Result.RemotePort)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Interface Alias: $($Result.InterfaceAlias)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Source Interface Address: $($Result.SourceAddress.IPAddress)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Succeeded: $($Result.PingSucceeded)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) Status: $($Result.PingReplyDetails.Status)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Ping Reply Time (RTT) RoundTripTime: $($Result.PingReplyDetails.RoundtripTime)"
                        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "TCPTestSucceeded: $($Result.TcpTestSucceeded)"
                    }
                }
            } # End Foreach Port in OptionalADPorts
            
        } # End Foreach Destination
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished local network port tests."
    }
} # End Function Network

# Test local DNS resolution for domain controllers
function Dns
{
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting local DNS resolution tests."
    If (!$ForestFQDN)
    {
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Local Dns resolution, you must specify for Active Directory Forest FQDN." -ConsoleOutput
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Local Dns resolution, you must specify for Active Directory Forest FQDN." -ConsoleOutput
    }
    
    If (!$DCs)
    {
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Local DNS resolution testing requires the DCs parameter to be specified." -ConsoleOutput
        Break
    }
    # Attempt DNS Resolution
    $DnsTargets = @("_ldap._tcp.$ForestFQDN") + $DCs
    Foreach ($HostName in $DnsTargets)
    {
        Try
        {
            $DnsResult = (Resolve-DnsName -Type ANY $HostName -ea Stop -wa SilentlyContinue)
            If ($DnsResult.Name)
            {
                Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Successfully resolved $($HostName)." -ConsoleOutput
            }
            Else
            {
                Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Error attempting DNS resolution for $($HostName)." -ConsoleOutput
                Write-Log -LogFile $Logfile -LogLevel ERROR -Message $DnsResult
            }
        }
        Catch
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Exception: Error attempting DNS resolution for $($HostName)." -ConsoleOutput
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "$($_.Exception.Message)"
        }
        Finally
        {
            If ($DebugLogging)
            {
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "Debug log entry for $($HostName)."
                Write-Log -LogFile $Logfile -LogLevel DEBUG -Message $DnsResult
            }
        }
    }
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished local DNS resolution tests."
} # End function Dns

function ActiveDirectory
{
    # Install if -InstallModules switch was used
    If (!(Get-Module -ListAvailable ActiveDirectory) -and $InstallModules)
    {
        # Check if Elevated
        $wid = [system.security.principal.windowsidentity]::GetCurrent()
        $prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
        $adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
        if ($prp.IsInRole($adm))
        {
            Write-Log -LogFile $Logfile -LogLevel SUCCESS -ConsoleOutput -Message "Elevated PowerShell session detected. Continuing."
        }
        else
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -ConsoleOutput -Message "This application/script must be run in an elevated PowerShell window. Please launch an elevated session and try again."
            Break
        }
        Try { $Result = Add-WindowsFeature Rsat-Adds }
        Catch
        {
            Write-Log -LogFile $Logfile -ConsoleOutput -Message "Error adding Windows Feature Rsat-Adds."
            Write-Log -LogFile $Logfile -Message "Exception: $($_.Exception.Message)"
        }
        Finally
        {
            Switch ($Result.Success)
            {
                True { Write-Log -LogFile $Logfile -ConsoleOutput -LogLevel SUCCESS -Message "Remote Server Administration Tools for Active Directory Domain Services installation completed successfully." }
                False { Write-Log -LogFile $Logfile -ConsoleOutput -Loglevel ERROR -Message "Remote Server Administration Tools for Active Directory Domain Services installation failed." }
            }
        }
    }
    If (Get-Module -ListAvailable ActiveDirectory)
    {
        Write-Log -LogFile $Logfile -Loglevel INFO -Message "Starting Active Directory tests."
        # Forest Configuration
        [string]$ForestMode = (Get-ADForest $ForestFQDN).ForestMode
        switch ($ForestMode)
        {
            Windows2000Forest { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Forest is Windows 2000 mode. Unsupported. Upgrade forest functional level." -ConsoleOutput }
            Windows2003Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows Server 2003 mode. Supported." }
            Windows2003InterimForest { Write-Log -Logfile $Logfile -Loglevel ERROR -Message "Windows Server 2003 interim function mode. Unsupported. Upgrade forest functional level." }
            Windows2008Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows Server 2008 mode. Supported." }
            Windows2008R2Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows Server 2008 R2 mode. Supported." }
            Windows2012R2Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows Server 2012 R2 mode. Supported." }
            Windows8Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows 8 mode. Supported." }
            Windows2012Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows Server 2012 Mode. Supported." }
            Windows2016Forest { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Forest is Windows Server 2016 Mode. Supported."}
        }
        
        # Forest and domain character checks
        [string]$ForestNetBIOS = (Get-ADForest $ForestFQDN).NetBIOSName
        [string]$DomainNetBIOS = (Get-ADDomain).NetBIOSName
        
        If ($ForestNetBIOS -match "\." -or $DomainNetBIOS -match "\.")
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Domain NetBIOS name contains a period. AAD Connect cannot be installed in this environment." -ConsoleOutput
        }
        Else { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Domain NetBIOS name does not contain a period. Passed." }
        
        # AD Recycle Bin
        If (Get-Command Get-ADOptionalFeature -ea silentlycontinue)
        {
            $RecycleBin = (Get-ADOptionalFeature -Filter { name -eq "Recycle Bin Feature" })
        }
        If (!($RecycleBin.EnabledScopes))
        {
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "AD Recycle Bin IS NOT ENABLED. It is recommended to enable the AD Recycle Bin."
            Write-Log -LogFile $Logfile -LogLevel INFO -Message "To enable, run Enable-ADOptionalFeature -'Recycle Bin Feature' -Scope ForestOrConfigurationSet -Target $($ForestFQDN)"
        }
        Else { Write-Log -LogFile $Logfile -LogLevel INFO -Message "AD Recycle Bin is ENABLED." }
        
        # Check for Read-Only Domain Controllers
        $CurrentSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().Name
        $RODCsInSite = @(Get-ADDomainController -Filter {Site eq "$CurrentSite" -and IsReadOnly -eq $true})
        If ($RODCcsInSite -ge 1)
        {
            Write-Log -LogFile $Logfile -LogLevel WARN -Message "Current site may contain Read-Only Domain Controllers. Read-Only Domain Controllers are not permitted for writeback operations or Password Hash Sync. Please verify if any DCs in the site $($CurrentSite) are Read-Only Domain Controllers."
        }
    }
    Else { Write-Log -LogFile $Logfile -LogLevel WARN -Message "Active Directory Module is not loaded. Please install using Install-WindowsFeature RSAT-ADDS or the -InstallModules switch." }
} # End function ActiveDirectory

function SystemConfiguration
{
    ## Show system configuration
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting system configuration gathering."
    [string]$OSVersion = ([System.Environment]::OSVersion.Version.Major.ToString() + "." + [System.Environment]::OSVersion.Version.Minor.ToString())
    [string]$OperatingSystem = (Get-WmiObject -Class Win32_OperatingSystem -Namespace "root\cimv2").Caption
    [string]$OSBitness = [System.Environment]::Is64BitOperatingSystem
    [string]$OSMachineName = [System.Environment]::MachineName.ToString()
    [string]$OperatingSystemSKU = (Get-WmiObject -Class Win32_OperatingSystem).OperatingSystemSKU
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "System name: $($OSMachineName)"
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "64-bit operating system detected: $($OSBitness)"
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System: $($OperatingSystem) $($OSVersion)"
    Switch ($OperatingSystemSKU)
    {
        0 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Undefined. Unable to determine operating system type. Azure AD Connect installation will probably fail." }
        1 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Ultimate. Installation not supported." }
        2 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Home Basic Edition. Installation not supported." }
        3 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Home Premium Edition. Installation not supported." }
        4 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Enterprise Edition. Installation not supported." }
        6 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Business Edition. Installation not supported." }
        7 { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System Edition is Standard Server. Installation may be supported if the Operating System version is supported." }
        8 { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System Edition is Datacenter Server. Installation may be supported if the Operating System version is supported." }
        9 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Small Business Server. Installation not supported." }
        10 { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System Edition is Enterprise Server. Installation may be supported if the Operating System version is supported." }
        11 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Starter. Installation not supported." }
        12 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Datacenter Server Core. Installation not supported." }
        13 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Standard Server Core. Installation not supported." }
        14 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Enterprise Server Core. Installation not supported." }
        17 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Web Server. Installation not supported." }
        19 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Home Server. Installation not supported." }
        20 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Express Server. Installation not supported." }
        21 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Standard Server. Installation not supported." }
        22 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Workgroup Server. Installation not supported." }
        23 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Enterprise Server. Installation not supported." }
        24 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Server for Small Business. Installation not supported." }
        25 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Small Business Server Premium. Installation not supported." }
        27 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Enterprise N. Installation not supported." }
        28 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Ultimate N. Installation not supported." }
        29 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Web Server Core. Installation not supported." }
        36 { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System Edition is Standard Server (without Hyper-V). Installation may be supported if Operating System version is supported." }
        37 { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System Edition is Datacenter Server (without Hyper-V). Installation may be supported if Operating System version is supported." }
        38 { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Operating System Edition is Enterprise Server (without Hyper-V). Installation may be supported if Operating System version is supported." }
        39 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Datacenter Core Server (without Hyper-V). Installation not supported." }
        40 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Standard Core Server (without Hyper-V). Installation not supported." }
        41 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Enterprise Core Server (without Hyper-V). Installation not supported." }
        42 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Hyper-V Server. Installation not supported." }
        43 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Server Express (Server Core). Installation not supported." }
        44 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Server Standard (Server Core). Installation not supported." }
        45 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Server Workgroup (Server Core). Installation not supported." }
        45 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Storage Server Enterprise (Server Core). Installation not supported." }
        50 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Server Essentials. Installation not supported." }
        63 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Small Business Server Premium (Server Core). Installation not supported." }
        64 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Computer Cluster Server (without Hyper-V). Installation not supported." }
        97 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Windows RT. Installation not supported." }
        101 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Home. Installation not supported." }
        103 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Professional with Media Center. Installation not supported." }
        104 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Mobile. Installation not supported." }
        123 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is IoT (Internet of Things) Core. Installation not supported." }
        143 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Datacenter Server (Nano). Installation not supported." }
        144 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Standard Server (Nano). Installation not supported." }
        147 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Datacenter Server (Server Core). Installation not supported." }
        148 { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Operating System Edition is Standard Server (Server Core). Installation not supported." }
        default {Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Unable to determine Operating System Edition SKU value."}
    }
    
    # Netsh WinHTTP proxy
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "WinHTTP proxy settings (netsh winhttp show proxy):"
    $WinHTTPProxy = (netsh winhttp show proxy)
    $WinHTTPProxy = ($WinHTTPProxy -join " ").Trim()
    Write-Log -LogFile $Logfile -LogLevel INFO -Message $WinHTTPProxy
    
    # .NET Proxy
    Write-Log -LogFile $Logfile -LogLevel INFO -Message ".NET proxy settings (machine.config/configuration/system.net/defaultproxy):"
    [xml]$machineconfig = gc $env:windir\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config
    if (!$machineconfig.configuration.'system.net'.defaultproxy)
    {
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "No proxy configuration exists in $env:windir\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config."
    }
    else
    {
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "The following proxy configuration exists in $env:windir\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config."
        $nodes = $machineconfig.ChildNodes.SelectNodes("/configuration/system.net/defaultproxy/proxy") | Sort -Unique
        Write-Log -Logfile $Logfile -LogLevel INFO -Message "UseSystemDefault: $($nodes.usesystemdefault)"
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "ProxyAddress: $($nodes.proxyaddress)"
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "BypassOnLocal $($nodes.bypassonlocal)"
    }
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "For more .NET proxy configuration parameters, see https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/network/proxy-element-network-settings"
    
    # .NET Framework Versions
    $NetFrameWorkVersion = (Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" -ErrorAction SilentlyContinue).Release.ToString()
    switch ($NetFrameWorkVersion)
    {
        { $NetFrameWorkVersion -ge 461808 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.7.2 or greater.";Break }
        { $NetFrameWorkVersion -ge 461308 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.7.1 or greater."; Break }
        { $NetFrameWorkVersion -ge 460798 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.7 or greater."; Break }
        { $NetFrameWorkVersion -ge 394802 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.6.2 or greater."; Break }
        { $NetFrameWorkVersion -ge 394254 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.6.1 or greater."; Break }
        { $NetFrameWorkVersion -ge 393295 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.6 or greater."; Break }
        { $NetFrameWorkVersion -ge 379893 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.5.2 or greater."; Break }
        { $NetFrameWorkVersion -ge 378675 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.5.1 or greater."; Break }
        { $NetFrameWorkVersion -ge 378389 } { Write-Log -LogFile $Logfile -LogLevel INFO -Message "The version of .NET Framework installed is 4.5 or greater."
                                              Write-Log -LogFile $Logfile -LogLevel WARN -Message "In order to install AAD Connect, upgrade to at least .NET Framework 4.5.1.";    Break }
        default { Write-Log -LogFile $Logfile -LogLevel WARN -Message "Unable to determine version of .NET Framework. AAD Connect requires .NET Framework 4.5.1 or greater.";  Break}
    }
    
    # Check service packs
    $ServicePack = (Get-ItemProperty "HKLM\Software\Microsoft\Windows NT\CurrentVersion" -ea silentlycontinue).ServicePack
    switch ($OSVersion)
    {
        "6.1" { If (!$ServicePack) { Write-Log -LogFile $Logfile -LogLevel WARN -Message "Windows Server 2008 R2 requires Service Pack 1 if Password Hash Synchronization will be used.";  } }
        default { Write-Log -LogFile $Logfile -LogLevel INFO -Message "No service packs are required for this version of Windows."}    
    }
    
    # Check PowerShell Versions
    switch ($OSVersion)
    {
        "6.0" { If ($PSVersionTable.Major -lt 3) { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Windows Server 2008 requires Windows Management Framework 3.0 or higher.";  } }
        "6.1" { If ($PSVersionTable.Major -lt 4) { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Windows Server 2008 R2 requires Windows Management Framework 4.0 or higher."; } }
        "6.2" { If ($PSVersionTable.Major -lt 4) { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Windows Server 2012 requires Windows Management Framework 4.0 or higher."; } }
        "10.0" { Write-Log -LogFile $Logfile -LogLevel INFO -Message "Windows Server 2016 or Windows Server 2019 have the required PowerShell version."}
    }
    
    # Server Features parameters
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Attempting to check installed features."
    If (Get-Command Get-WindowsFeature -ErrorAction SilentlyContinue)
    {
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Command available. Checking installed features."
        $ServerFeatures = Get-WindowsFeature | ? {
            $_.Name -eq 'Server-Gui-Mgmt-Infra' -or `
            $_.Name -eq 'Server-Gui-Shell' -or `
            $_.Name -eq 'NET-Framework-45-Features' -or `
            $_.Name -eq 'NET-Framework-45-Core'
        }
        foreach ($Feature in $ServerFeatures)
        {
            switch ($Feature.InstallState)
            {
                Installed { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "Required feature $($Feature.DisplayName) [$($Feature.Name)] is installed." }
                Available { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "Required feature $($Feature.DisplayName) [$($Feature.Name)] is not installed." }
            } # End Switch FeatureIsInstalled
        } # End Foreach Feature
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished checking installed features."
    } # End Server Feaatures
    Else { Write-Log -LogFile $Logfile -LogLevel WARN -Message "Command not available. Unable to check installed features." }
    
    # Check for TLS capabilities
    switch ($OSVersion)
    {
        "10.0"    {
            switch -regex ($OperatingSystem)
            {
                "(?i)(Server)" {
                    Write-Log -Logfile $Logfile -ConsoleOutput -LogLevel INFO -Message "Checking TLS settings for Windows Server 2016 and Windows Server 2019."
                    $KeysArray = @(
                        @{ Path = "HKLM:SOFTWARE\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" },
                        @{ Path = "HKLM:SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" }
                    )
                }
                default
                {
                    Write-Log -LogFile $Logfile -ConsoleOutput -LogLevel INFO -Message "Desktop operating system is not a candidate for AAD Connect Installation."    
                }
            }
        } # End 10.0 / Windows Server 2016 / 2019
        
        "6.3"    {
                    Write-Log -Logfile $Logfile -ConsoleOutput -LogLevel INFO -Message "Checking TLS settings for Windows Server 2012 R2."
                    $KeysArray = @(
                        @{ Path = "HKLM:SOFTWARE\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" },
                        @{ Path = "HKLM:SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" }
                    )
                } # End 6.3 / Windows Server 2012 R2
        
        "6.2"    {
                    Write-Log -Logfile $Logfile -ConsoleOutput -LogLevel INFO -Message "Checking TLS settings for Windows Server 2012."
                    $KeysArray = @(
                        @{ Path = "HKLM:SOFTWARE\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" },
                        @{ Path = "HKLM:SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" }
                    )
                } # End 6.2 / Windows Server 2012
        
        "6.1"    {
                    Write-Log -Logfile $Logfile -ConsoleOutput -LogLevel INFO -Message "Checking TLS settings for Windows Server 2008 R2."
                    $KeysArray = @(
                        @{ Path = "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client"; Item = "DisabledByDefault"; Type = "REG_DWORD"; Value = "0" },
                        @{ Path = "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client"; Item = "Enabled"; type = "REG_DWORD"; Value = "1" },
                        @{ Path = "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server"; Item = "DisabledByDefault"; type = "REG_DWORD"; Value = "0" },
                        @{ Path = "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server"; Item = "Enabled"; type = "REG_DWORD"; Value = "1" },
                        @{ Path = "HKLM:SOFTWARE\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" },
                        @{ Path = "HKLM:SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319"; Item = "SchUseStrongCrypto"; type = "REG_DWORD"; Value = "1" }
                        )
                } # End 6.1 / Windows Server 2008 R2

        "6.0"    { 
                    Write-Log -Logfile $Logfile -ConsoleOutput -LogLevel INFO -Message "Checking TLS settings for Windows Server 2008."
                    Write-Log -LogFile $Logfile -ConsoleOutput -LogLevel WARN -Message "TLS 1.2 cannot be enabled on Windows Server 2008."
                } # End 6.0 / Windows Server 2008
        
        default { Write-Log -LogFile $Logfile -ConsoleOutput -LogLevel WARN -Message "Unable to determine Windows Version. TLS checks will be skipped." }
    }
    
    If ($KeysArray)
    {
        foreach ($Key in $KeysArray)
        {
            try
            {
                $Result = (Get-ItemProperty -erroraction SilentlyContinue $Key.Path).$($Key.Item).ToString()
                If ($Result)
                {
                    If ($Result -match $Key.Value)
                    {
                        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Key $($Key.Path)\$($Key.Item) with a value of $($Key.Value) is set correctly for TLS 1.2 Configuration."
                    }
                    Else
                    {
                        $RegKeyPath = ($Key.Path).Replace(":", "\")
                        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Key $($Key.Path)\$($Key.Item) with a value of $($Key.Value) is not set correctly for TLS 1.2 Configuration."
                        Write-Log -LogFile $LogFile -LogLevel INFO -Message "Key $($Key.Path)\$($Key.Item) must be set to $($Key.Value) for TLS 1.2 support."
                        Write-Log -LogFile $Logfile -LogLevel INFO -Message "To configure, run: REG ADD ""$($RegKeyPath)"" /v $($Key.Item) /d $($Key.Value) /t REG_DWORD /f"
                    }
                }
                Else
                {
                    $RegKeyPath = ($Key.Path).Replace(":", "\")
                    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Key $($Key.Path)\$($Key.Item) not found."
                    Write-Log -LogFile $LogFile -LogLevel INFO -Message "Key $($Key.Path)\$($Key.Item) must be set to $($Key.Value) for TLS 1.2 support."
                    Write-Log -LogFile $Logfile -LogLevel INFO -Message "To configure, run: REG ADD ""$($RegKeyPath)"" /v $($Key.Item) /d $($Key.Value) /t REG_DWORD /f"
                }
            }
            Catch
            {
                $RegKeyPath = ($Key.Path).Replace(":", "\")
                Write-Log -LogFile $Logfile -LogLevel INFO -Message "Exception or $($Key.Path)\$($Key.Item) not found."
                Write-Log -LogFile $LogFile -LogLevel INFO -Message "Key $($Key.Path)\$($Key.Item) must be set to $($Key.Value) for TLS 1.2 support."
                Write-Log -LogFile $Logfile -LogLevel INFO -Message "To configure, run: REG ADD ""$($RegKeyPath)"" /v $($Key.Item) /d $($Key.Value) /t REG_DWORD /f"
            }
        }
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished checking for TLS 1.2 Configuration settings."
    }
    
    # Check Group Policy PowerShell Transcription has been enabled
    Try { $GPOTranscription = (Get-ItemProperty "HKLM:SOFTWARE\Policies\Microsoft\Windows\PowerShell\Transcription\" -ErrorAction SilentlyContinue).EnableTranscripting.ToString() }
    Catch { }
    If ($GPOTranscription)
    {
        switch ($GPOTranscription)
        {
            0 { $Value = "disabled" }; 1 { $Value = "enabled" }
        }
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "PowerShell transcription is configured through Group Policy. The current value is $($GPOTranscription) ($($Value))." -ConsoleOutput
        Write-Log -LogFile $Logfile -LogLevel ERROR -Message "PowerShell transcription group policy must be set to 'Disabled' or 'Not Configured' or the installation process will fail." -ConsoleOutput
        Write-Log -LogFile $Logfile -LogLevel INFO -Message "PowerShell transcription detected. Attempting to determine which policy has it enabled." -ConsoleOutput
        
        # Check if Elevated
        $wid = [system.security.principal.windowsidentity]::GetCurrent()
        $prp = New-Object System.Security.Principal.WindowsPrincipal($wid)
        $adm = [System.Security.Principal.WindowsBuiltInRole]::Administrator
        if ($prp.IsInRole($adm))
        {
            Write-Log -LogFile $Logfile -LogLevel SUCCESS -ConsoleOutput -Message "Elevated PowerShell session detected. Continuing."
            $RsopFile = (Get-Date -Format yyyy-MM-dd) + "_AADConnectConnectivityRSOP.txt"
            gpresult /f /SCOPE Computer /X $RsopFile
            [xml]$Rsop = gc $RsopFile
            [array]$data = ($Rsop.Rsop.ComputerResults | ? { $_.InnerXml -like '*transcription*' } | Select -ExpandProperty GPO).Name
            foreach ($policy in $data) { Write-Log -LogFile $Logfile -LogLevel ERROR -Message "The Group Policy Object $policy has a value configured for Turn on PowerShell Transcription. This will cause AAD Connect installation to fail." -ConsoleOutput}
        }
        else
        {
            Write-Log -LogFile $Logfile -LogLevel ERROR -ConsoleOutput -Message "Unable to export group policy information without elevation. Please launch an elevated session and try again."
        }
        
    }
    If (!$GPOTranscription) { Write-Log -LogFile $Logfile -LogLevel SUCCESS -Message "PowerShell transcription is not configured." }
    
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Finished gathering system configuration."
} # End Function System Configuration

## Begin script
Write-Log -LogFile $Logfile -LogLevel INFO -Message "========================================================="
Write-Log -LogFile $Logfile -LogLevel INFO -Message "Starting AAD Connect connectivity and resolution testing."

# If SkipDcDnsPortCheck is enabled, remove 53 from the list of ports to test on DCs
If ($SkipDcDnsPortCheck) { $Ports = @('135', '389', '445', '3268') }
Else { $Ports = @('53', '135', '389', '445', '3268') }

# Use this switch if a statically configured Rpc port for AD traffic has been configured
# on the target DC. This port may be called for Password Hash Sync configuration.
If ($FixedDcRpcPort)
{
    $Ports += $FixedDcRpcPort
    Write-Log -LogFile $Logfile -LogLevel INFO -Message "Port $($FixedDcRpcPort) will be tested as part of the DC/local network test."
    If ($DebugLogging)
    {
        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "For more information on configuring a fixed RPC port for DC communications, please see"
        Write-Log -LogFile $Logfile -LogLevel DEBUG -Message "https://support.microsoft.com/en-us/help/224196/restricting-active-directory-rpc-traffic-to-a-specific-port"
    }
}

# Use the OptionalADPortTest switch to add the following ports: 88, 636, 3269
# In order to use ports 636 and 3269, domain controllers must be configured with a
# valid server certificate. See https://social.technet.microsoft.com/wiki/contents/articles/18254.ldaps-636-and-msft-gc-ssl-3269-service.aspx
# and https://social.technet.microsoft.com/wiki/contents/articles/2980.ldap-over-ssl-ldaps-certificate.aspx.
If ($OptionalADPortTest) { $OptionalADPorts += @('88', '636','3269') }

If ($AllTests -or $PSBoundParameters.Count -eq 0)
{
    If (!$PSBoundParameters.ContainsKey("AzureCredentialCheck")) { $AzureCredentialCheck = $true }
    If (!$PSBoundParameters.ContainsKey("Dns")) { $Dns = $true }
    If (!$PSBoundParameters.ContainsKey("Network")) { $Network = $true }
    If (!$PSBoundParameters.ContainsKey("OnlineEndPoints")) { $OnlineEndPoints = $true }
    If (!$PSBoundParameters.ContainsKey("ActiveDirectory")) { $ActiveDirectory = $true }
    If (!$PSBoundParameters.ContainsKey("SystemConfiguration")) { $SystemConfiguration = $true }
    If (!$PSBoundParameters.ContainsKey("OptionalFeatureCheck")) { $OptionalFeatureCheck = "PasswordWriteBack"}
}

If ($AzureCredentialCheck) { AzureCredential }
If ($Dns) { Dns }
If ($Network) { Network }
If ($OnlineEndPoints) { OnlineEndPoints }
If ($OptionalFeatureCheck) { OptionalFeatureCheck }
If ($ActiveDirectory) { ActiveDirectory }
If ($SystemConfiguration) { SystemConfiguration }

Write-Log -LogFile $Logfile -LogLevel INFO -Message "Done! Logfile is $($Logfile)." -ConsoleOutput
Write-Log -LogFile $Logfile -LogLevel INFO -Message "---------------------------------------------------------"