-
Notifications
You must be signed in to change notification settings - Fork 664
Expand file tree
/
Copy pathValidate-NDESConfiguration.ps1
More file actions
1686 lines (1099 loc) · 65.7 KB
/
Validate-NDESConfiguration.ps1
File metadata and controls
1686 lines (1099 loc) · 65.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Highlights configuration problems on an NDES server, as configured for use with Intune Standalone SCEP certificates.
.DESCRIPTION
Validate-NDESConfig looks at the configuration of your NDES server and ensures it aligns to the "Configure and manage SCEP
certificates with Intune" article.
.NOTE This script is used purely to validate the configuration. All remedial tasks will need to be carried out manually.
Where possible, a link and section description will be provided.
.EXAMPLE
.\Validate-NDESConfiguration -NDESServiceAccount Contoso\NDES_SVC.com -IssuingCAServerFQDN IssuingCA.contoso.com -SCEPUserCertTemplate SCEPGeneral
.EXAMPLE
.\Validate-NDESConfiguration -help
.LINK
https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure
#>
[CmdletBinding(DefaultParameterSetName="NormalRun")]
Param(
[parameter(Mandatory=$true,ParameterSetName="NormalRun")]
[alias("sa")]
[ValidateScript({
if ($_ -match ".\\."){
$True
}
else {
Throw "Please use the format Domain\Username for the NDES Service Account variable."
}
$EnteredDomain = $_.split("\")
$ads = New-Object -ComObject ADSystemInfo
$Domain = $ads.GetType().InvokeMember('DomainShortName','GetProperty', $Null, $ads, $Null)
if ($EnteredDomain -like "$Domain") {
$True
}
else {
Throw "Incorrect Domain. Ensure domain is '$($Domain)\<USERNAME>'"
}
}
)]
[string]$NDESServiceAccount,
[parameter(Mandatory=$true,ParameterSetName="NormalRun")]
[alias("ca")]
[ValidateScript({
$Domain = (Get-WmiObject Win32_ComputerSystem).domain
if ($_ -match $Domain) {
$True
}
else {
Throw "The Network Device Enrollment Server and the Certificate Authority are not members of the same Active Directory domain. This is an unsupported configuration."
}
}
)]
[string]$IssuingCAServerFQDN,
[parameter(Mandatory=$true,ParameterSetName="NormalRun")]
[alias("t")]
[string]$SCEPUserCertTemplate,
[parameter(ParameterSetName="Help")]
[alias("h","?","/?")]
[switch]$help,
[parameter(ParameterSetName="Help")]
[alias("u")]
[switch]$usage
)
#######################################################################
Function Log-ScriptEvent {
[CmdletBinding()]
Param(
[parameter(Mandatory=$True)]
[String]$LogFilePath,
[parameter(Mandatory=$True)]
[String]$Value,
[parameter(Mandatory=$True)]
[String]$Component,
[parameter(Mandatory=$True)]
[ValidateRange(1,3)]
[Single]$Severity
)
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime
$DateTime.SetVarDate($(Get-Date))
$UtcValue = $DateTime.Value
$UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21)
$LogLine = "<![LOG[$Value]LOG]!>" +`
"<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" " +`
"date=`"$(Get-Date -Format M-d-yyyy)`" " +`
"component=`"$Component`" " +`
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " +`
"type=`"$Severity`" " +`
"thread=`"$([Threading.Thread]::CurrentThread.ManagedThreadId)`" " +`
"file=`"`">"
Add-Content -Path $LogFilePath -Value $LogLine
}
##########################################################################################################
function Show-Usage {
Write-Host
Write-Host "-help -h Displays the help."
Write-Host "-usage -u Displays this usage information."
Write-Host "-NDESExternalHostname -ed External DNS name for the NDES server (SSL certificate subject will be checked for this. It should be in the SAN of the certificate if"
write-host " clients communicate directly with the NDES server)"
Write-Host "-NDESServiceAccount -sa Username of the NDES service account. Format is Domain\sAMAccountName, such as Contoso\NDES_SVC."
Write-Host "-IssuingCAServerFQDN -ca Name of the issuing CA to which you'll be connecting the NDES server. Format is FQDN, such as 'MyIssuingCAServer.contoso.com'."
Write-Host "-SCEPUserCertTemplate -t Name of the SCEP Certificate template. Please note this is _not_ the display name of the template. Value should not contain spaces."
Write-Host
}
#######################################################################
function Get-NDESHelp {
Write-Host
Write-Host "Verifies if the NDES server meets all the required configuration. "
Write-Host
Write-Host "The NDES server role is required as back-end infrastructure for Intune Standalone for delivering VPN and Wi-Fi certificates via the SCEP protocol to mobile devices and desktop clients."
Write-Host "See https://docs.microsoft.com/en-us/intune/certificates-scep-configure."
Write-Host
}
#######################################################################
if ($help){
Get-NDESHelp
break
}
if ($usage){
Show-Usage
break
}
#######################################################################
#Requires -version 3.0
#Requires -RunAsAdministrator
#######################################################################
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name) | Out-Null
$TempDirPath = "$parent$name"
$LogFilePath = "$($TempDirPath)\Validate-NDESConfig.log"
#######################################################################
#region Proceed with Variables...
Write-Host
Write-host "......................................................."
Write-Host
Write-Host "NDES Service Account = "-NoNewline
Write-Host "$($NDESServiceAccount)" -ForegroundColor Cyan
Write-host
Write-Host "Issuing CA Server = " -NoNewline
Write-Host "$($IssuingCAServerFQDN)" -ForegroundColor Cyan
Write-host
Write-Host "SCEP Certificate Template = " -NoNewline
Write-Host "$($SCEPUserCertTemplate)" -ForegroundColor Cyan
Write-Host
Write-host "......................................................."
Write-Host
Write-Host "Proceed with variables? [Y]es, [N]o"
$confirmation = Read-Host
#endregion
#######################################################################
if ($confirmation -eq 'y'){
Write-Host
Write-host "......................................................."
Log-ScriptEvent $LogFilePath "Initializing log file $($TempDirPath)\Validate-NDESConfig.log" NDES_Validation 1
Log-ScriptEvent $LogFilePath "Proceeding with variables=YES" NDES_Validation 1
Log-ScriptEvent $LogFilePath "NDESServiceAccount=$($NDESServiceAccount)" NDES_Validation 1
Log-ScriptEvent $LogFilePath "IssuingCAServer=$($IssuingCAServerFQDN)" NDES_Validation 1
Log-ScriptEvent $LogFilePath "SCEPCertificateTemplate=$($SCEPUserCertTemplate)" NDES_Validation 1
#######################################################################
#region Install RSAT tools, Check if NDES and IIS installed
if (-not (Get-WindowsFeature ADCS-Device-Enrollment).Installed){
Write-Host "Error: NDES Not installed" -BackgroundColor Red
write-host "Exiting....................."
Log-ScriptEvent $LogFilePath "NDES Not installed" NDES_Validation 3
break
}
Install-WindowsFeature RSAT-AD-PowerShell | Out-Null
Import-Module ActiveDirectory | Out-Null
if (-not (Get-WindowsFeature Web-WebServer).Installed){
$IISNotInstalled = $TRUE
Write-Warning "IIS is not installed. Some tests will not run as we're unable to import the WebAdministration module"
Write-Host
Log-ScriptEvent $LogFilePath "IIS is not installed. Some tests will not run as we're unable to import the WebAdministration module" NDES_Validation 2
}
else {
Import-Module WebAdministration | Out-Null
}
#endregion
#######################################################################
#region checking OS version
Write-Host
Write-host "Checking Windows OS version..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking OS Version" NDES_Validation 1
$OSVersion = (Get-CimInstance -class Win32_OperatingSystem).Version
$MinOSVersion = "6.3"
if ([version]$OSVersion -lt [version]$MinOSVersion){
Write-host "Error: Unsupported OS Version. NDES Requires 2012 R2 and above." -BackgroundColor Red
Log-ScriptEvent $LogFilePath "Unsupported OS Version. NDES Requires 2012 R2 and above." NDES_Validation 3
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "OS Version " -NoNewline
write-host "$($OSVersion)" -NoNewline -ForegroundColor Cyan
write-host " supported."
Log-ScriptEvent $LogFilePath "Server is version $($OSVersion)" NDES_Validation 1
}
#endregion
#######################################################################
#region Checking NDES Service Account properties in Active Directory
Write-host
Write-host "......................................................."
Write-Host
Write-host "Checking NDES Service Account properties in Active Directory..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking NDES Service Account properties in Active Directory" NDES_Validation 1
$ADUser = $NDESServiceAccount.split("\")[1]
$ADUserProps = (Get-ADUser $ADUser -Properties SamAccountName,enabled,AccountExpirationDate,accountExpires,accountlockouttime,PasswordExpired,PasswordLastSet,PasswordNeverExpires,LockedOut)
if ($ADUserProps.enabled -ne $TRUE -OR $ADUserProps.PasswordExpired -ne $false -OR $ADUserProps.LockedOut -eq $TRUE){
Write-Host "Error: Problem with the AD account. Please see output below to determine the issue" -BackgroundColor Red
Write-Host
Log-ScriptEvent $LogFilePath "Problem with the AD account. Please see output below to determine the issue" NDES_Validation 3
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "NDES Service Account seems to be in working order:"
Log-ScriptEvent $LogFilePath "NDES Service Account seems to be in working order" NDES_Validation 1
}
Get-ADUser $ADUser -Properties SamAccountName,enabled,AccountExpirationDate,accountExpires,accountlockouttime,PasswordExpired,PasswordLastSet,PasswordNeverExpires,LockedOut | fl SamAccountName,enabled,AccountExpirationDate,accountExpires,accountlockouttime,PasswordExpired,PasswordLastSet,PasswordNeverExpires,LockedOut
#endregion
#######################################################################
#region Checking if NDES server is the CA
Write-host "`n.......................................................`n"
Write-host "Checking if NDES server is the CA...`n" -ForegroundColor Yellow
Log-ScriptEvent $LogFilePath "Checking if NDES server is the CA" NDES_Validation 1
$hostname = ([System.Net.Dns]::GetHostByName(($env:computerName))).hostname
$CARoleInstalled = (Get-WindowsFeature ADCS-Cert-Authority).InstallState -eq "Installed"
if ($hostname -match $IssuingCAServerFQDN){
Write-host "Error: NDES is running on the CA. This is an unsupported configuration!" -BackgroundColor Red
Log-ScriptEvent $LogFilePath "NDES is running on the CA" NDES_Validation 3
}
elseif($CARoleInstalled)
{
Write-host "Error: NDES server has Certification Authority Role installed. This is an unsupported configuration!" -BackgroundColor Red
Log-ScriptEvent $LogFilePath "NDES server has Certification Authority Role installed" NDES_Validation 3
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "NDES server is not running on the CA"
Log-ScriptEvent $LogFilePath "NDES server is not running on the CA" NDES_Validation 1
}
#endregion
#######################################################################
#region Checking NDES Service Account local permissions
Write-host
Write-host "......................................................."
Write-host
Write-host "Checking NDES Service Account local permissions..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking NDES Service Account local permissions" NDES_Validation 1
if ((net localgroup) -match "Administrators"){
$LocalAdminsMember = ((net localgroup Administrators))
if ($LocalAdminsMember -like "*$NDESServiceAccount*"){
Write-Warning "NDES Service Account is a member of the local Administrators group. This will provide the requisite rights but is _not_ a secure configuration. Use IIS_IUSERS instead."
Log-ScriptEvent $LogFilePath "NDES Service Account is a member of the local Administrators group. This will provide the requisite rights but is _not_ a secure configuration. Use IIS_IUSERS instead." NDES_Validation 2
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "NDES Service account is not a member of the Local Administrators group"
Log-ScriptEvent $LogFilePath "NDES Service account is not a member of the Local Administrators group" NDES_Validation 1
}
Write-host
Write-Host "Checking NDES Service account is a member of the IIS_IUSR group..." -ForegroundColor Yellow
Write-host
if ((net localgroup) -match "IIS_IUSRS"){
$IIS_IUSRMembers = ((net localgroup IIS_IUSRS))
if ($IIS_IUSRMembers -like "*$NDESServiceAccount*"){
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "NDES Service Account is a member of the local IIS_IUSR group" -NoNewline
Log-ScriptEvent $LogFilePath "NDES Service Account is a member of the local IIS_IUSR group" NDES_Validation 1
}
else {
Write-Host "Error: NDES Service Account is not a member of the local IIS_IUSR group" -BackgroundColor red
Log-ScriptEvent $LogFilePath "NDES Service Account is not a member of the local IIS_IUSR group" NDES_Validation 3
Write-host
Write-host "Checking Local Security Policy for explicit rights via gpedit..." -ForegroundColor Yellow
Write-Host
$TempFile = [System.IO.Path]::GetTempFileName()
& "secedit" "/export" "/cfg" "$TempFile" | Out-Null
$LocalSecPol = Get-Content $TempFile
$ADUserProps = Get-ADUser $ADUser
$NDESSVCAccountSID = $ADUserProps.SID.Value
$LocalSecPolResults = $LocalSecPol | Select-String $NDESSVCAccountSID
if ($LocalSecPolResults -match "SeInteractiveLogonRight" -AND $LocalSecPolResults -match "SeBatchLogonRight" -AND $LocalSecPolResults -match "SeServiceLogonRight"){
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "NDES Service Account has been assigned the Logon Locally, Logon as a Service and Logon as a batch job rights explicitly."
Log-ScriptEvent $LogFilePath "NDES Service Account has been assigned the Logon Locally, Logon as a Service and Logon as a batch job rights explicitly." NDES_Validation 1
Write-Host
Write-Host "Note:" -BackgroundColor Red -NoNewline
Write-Host " The Logon Locally is not required in normal runtime."
Write-Host
Write-Host "Note:" -BackgroundColor Red -NoNewline
Write-Host 'Consider using the IIS_IUSERS group instead of explicit rights as documented under "Step 1 - Create an NDES service account".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
}
else {
Write-Host "Error: NDES Service Account has _NOT_ been assigned the Logon Locally, Logon as a Service or Logon as a batch job rights _explicitly_." -BackgroundColor red
Write-Host 'Please review "Step 1 - Create an NDES service account".'
write-host "https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "NDES Service Account has _NOT_ been assigned the Logon Locally, Logon as a Service or Logon as a batch job rights _explicitly_." NDES_Validation 3
}
}
}
else {
Write-Host "Error: No IIS_IUSRS group exists. Ensure IIS is installed." -BackgroundColor red
write-host 'Please review "Step 3.1 - Configure prerequisites on the NDES server".'
write-host "https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "No IIS_IUSRS group exists. Ensure IIS is installed." NDES_Validation 3
}
}
else {
Write-Warning "No local Administrators group exists, likely due to this being a Domain Controller. It is not recommended to run NDES on a Domain Controller."
Log-ScriptEvent $LogFilePath "No local Administrators group exists, likely due to this being a Domain Controller. It is not recommended to run NDES on a Domain Controller." NDES_Validation 2
}
#endregion
#######################################################################
#region Checking Windows Features are installed.
Write-host
Write-Host
Write-host "......................................................."
Write-host
Write-host "Checking Windows Features are installed..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking Windows Features are installed..." NDES_Validation 1
$WindowsFeatures = @("Web-Filtering","Web-Net-Ext45","NET-Framework-45-Core","NET-WCF-HTTP-Activation45","Web-Metabase","Web-WMI")
foreach($WindowsFeature in $WindowsFeatures){
$Feature = Get-WindowsFeature $WindowsFeature
$FeatureDisplayName = $Feature.displayName
if($Feature.installed){
Write-host "Success:" -ForegroundColor Green -NoNewline
write-host "$FeatureDisplayName Feature Installed"
Log-ScriptEvent $LogFilePath "$($FeatureDisplayName) Feature Installed" NDES_Validation 1
}
else {
Write-Host "Error: $FeatureDisplayName Feature not installed!" -BackgroundColor red
Write-Host 'Please review "Step 3.1b - Configure prerequisites on the NDES server".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "$($FeatureDisplayName) Feature not installed" NDES_Validation 3
}
}
#endregion
#################################################################
#region Checking NDES Install Paramaters
$ErrorActionPreference = "SilentlyContinue"
Write-host
Write-host "......................................................."
Write-host
Write-Host "Checking NDES Install Paramaters..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking NDES Install Paramaters" NDES_Validation 1
$InstallParams = @(Get-WinEvent -LogName "Microsoft-Windows-CertificateServices-Deployment/Operational" | Where-Object {$_.id -eq "105"}|
Where-Object {$_.message -match "Install-AdcsNetworkDeviceEnrollmentService"}| Sort-Object -Property TimeCreated -Descending | Select-Object -First 1)
if ($InstallParams.Message -match '-SigningProviderName "Microsoft Strong Cryptographic Provider"' -AND ($InstallParams.Message -match '-EncryptionProviderName "Microsoft Strong Cryptographic Provider"')) {
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "Correct CSP used in install parameters"
Write-host
Write-Host $InstallParams.Message
Log-ScriptEvent $LogFilePath "Correct CSP used in install parameters:" NDES_Validation 1
Log-ScriptEvent $LogFilePath "$($InstallParams.Message)" NDES_Eventvwr 1
}
else {
Write-Host "Error: Incorrect CSP selected during install. NDES only supports the CryptoAPI CSP." -BackgroundColor red
Write-Host
Write-Host $InstallParams.Message
Log-ScriptEvent $LogFilePath "Error: Incorrect CSP selected during install. NDES only supports the CryptoAPI CSP" NDES_Validation 3
Log-ScriptEvent $LogFilePath "$($InstallParams.Message)" NDES_Eventvwr 3
}
$ErrorActionPreference = "Continue"
#endregion
#################################################################
#region Checking IIS Application Pool health
Write-host
Write-host "......................................................."
Write-host
Write-host "Checking IIS Application Pool health..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking IIS Application Pool health" NDES_Validation 1
if (-not ($IISNotInstalled -eq $TRUE)){
# If SCEP AppPool Exists
if (Test-Path 'IIS:\AppPools\SCEP'){
$IISSCEPAppPoolAccount = Get-Item 'IIS:\AppPools\SCEP' | select -expandproperty processmodel | select -Expand username
if ((Get-WebAppPoolState "SCEP").value -match "Started"){
$SCEPAppPoolRunning = $TRUE
}
}
else {
Write-Host "Error: SCEP Application Pool missing!" -BackgroundColor red
Write-Host 'Please review "Step 3.1 - Configure prerequisites on the NDES server"'.
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "SCEP Application Pool missing" NDES_Validation 3
}
if ($IISSCEPAppPoolAccount -contains "$NDESServiceAccount"){
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "Application Pool is configured to use " -NoNewline
Write-Host "$($IISSCEPAppPoolAccount)"
Log-ScriptEvent $LogFilePath "Application Pool is configured to use $($IISSCEPAppPoolAccount)" NDES_Validation 1
}
else {
Write-Host "Error: Application Pool is not configured to use the NDES Service Account" -BackgroundColor red
Write-Host 'Please review "Step 4.1 - Configure NDES for use with Intune".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "Application Pool is not configured to use the NDES Service Account" NDES_Validation 3
}
if ($SCEPAppPoolRunning){
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "SCEP Application Pool is Started " -NoNewline
Log-ScriptEvent $LogFilePath "SCEP Application Pool is Started" NDES_Validation 1
}
else {
Write-Host "Error: SCEP Application Pool is stopped!" -BackgroundColor red
Write-Host "Please start the SCEP Application Pool via IIS Management Console. You should also review the Application Event log output for Errors"
Log-ScriptEvent $LogFilePath "SCEP Application Pool is stopped" NDES_Validation 3
}
}
else {
Write-Host "IIS is not installed." -BackgroundColor red
Log-ScriptEvent $LogFilePath "SCEP Application Pool is stopped" NDES_Validation 3
}
#endregion
#################################################################
#region Checking Request Filtering
Write-Host
Write-host
Write-host "......................................................."
Write-host
Write-Host "Checking Request Filtering (Default Web Site -> Request Filtering -> Edit Feature Setting) has been configured in IIS..." -ForegroundColor Yellow
Write-Host
Log-ScriptEvent $LogFilePath "Checking Request Filtering" NDES_Validation 1
if (-not ($IISNotInstalled -eq $TRUE)){
[xml]$RequestFiltering = (c:\windows\system32\inetsrv\appcmd.exe list config "default web site" /section:requestfiltering)
if ($RequestFiltering.'system.webserver'.security.requestFiltering.requestLimits.maxQueryString -eq "65534"){
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "MaxQueryString Set Correctly"
Log-ScriptEvent $LogFilePath "MaxQueryString Set Correctly" NDES_Validation 1
}
else {
Write-Host "MaxQueryString not set correctly!" -BackgroundColor red
Write-Host 'Please review "Step 4.4 - Configure NDES for use with Intune".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "MaxQueryString not set correctly" NDES_Validation 3
}
if ($RequestFiltering.'system.webserver'.security.requestFiltering.requestLimits.maxUrl -eq "65534"){
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "MaxUrl Set Correctly"
Log-ScriptEvent $LogFilePath "MaxUrl Set Correctly" NDES_Validation 1
}
else {
Write-Host "maxUrl not set correctly!" -BackgroundColor red
Write-Host 'Please review "Step 4.4 - Configure NDES for use with Intune".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure'"
Log-ScriptEvent $LogFilePath "maxUrl not set correctly" NDES_Validation 3
}
}
else {
Write-Host "IIS is not installed." -BackgroundColor red
Log-ScriptEvent $LogFilePath "IIS is not installed" NDES_Validation 3
}
#endregion
#################################################################
#region Checking registry has been set to allow long URLs
Write-host
Write-host "......................................................."
Write-host
Write-Host 'Checking registry "HKLM:SYSTEM\CurrentControlSet\Services\HTTP\Parameters" has been set to allow long URLs...' -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking registry (HKLM:SYSTEM\CurrentControlSet\Services\HTTP\Parameters) has been set to allow long URLs" NDES_Validation 1
if (-not ($IISNotInstalled -eq $TRUE)){
If ((Get-ItemProperty -Path HKLM:SYSTEM\CurrentControlSet\Services\HTTP\Parameters -Name MaxFieldLength).MaxfieldLength -notmatch "65534"){
Write-Host "Error: MaxFieldLength not set to 65534 in the registry!" -BackgroundColor red
Write-Host
Write-Host 'Please review "Step 4.3 - Configure NDES for use with Intune".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "MaxFieldLength not set to 65534 in the registry" NDES_Validation 3
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "MaxFieldLength set correctly"
Log-ScriptEvent $LogFilePath "MaxFieldLength set correctly" NDES_Validation 1
}
if ((Get-ItemProperty -Path HKLM:SYSTEM\CurrentControlSet\Services\HTTP\Parameters -Name MaxRequestBytes).MaxRequestBytes -notmatch "65534"){
Write-Host "MaxRequestBytes not set to 65534 in the registry!" -BackgroundColor red
Write-Host
Write-Host 'Please review "Step 4.3 - Configure NDES for use with Intune".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure'"
Log-ScriptEvent $LogFilePath "MaxRequestBytes not set to 65534 in the registry" NDES_Validation 3
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "MaxRequestBytes set correctly"
Log-ScriptEvent $LogFilePath "MaxRequestBytes set correctly" NDES_Validation 1
}
}
else {
Write-Host "IIS is not installed." -BackgroundColor red
Log-ScriptEvent $LogFilePath "IIS is not installed." NDES_Validation 3
}
#endregion
#################################################################
#region Checking SPN has been set...
Write-host
Write-host "......................................................."
Write-host
Write-Host "Checking SPN has been set..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking SPN has been set" NDES_Validation 1
$hostname = ([System.Net.Dns]::GetHostByName(($env:computerName))).hostname
$spn = setspn.exe -L $ADUser
if ($spn -match $hostname){
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "Correct SPN set for the NDES service account:"
Write-host
Write-Host $spn -ForegroundColor Cyan
Log-ScriptEvent $LogFilePath "Correct SPN set for the NDES service account: $($spn)" NDES_Validation 1
}
else {
Write-Host "Error: Missing or Incorrect SPN set for the NDES Service Account!" -BackgroundColor red
Write-Host 'Please review "Step 3.1c - Configure prerequisites on the NDES server".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "Missing or Incorrect SPN set for the NDES Service Account" NDES_Validation 3
}
#endregion
#################################################################
#region Checking there are no intermediate certs are in the Trusted Root store
Write-host
Write-host "......................................................."
Write-host
Write-Host "Checking there are no intermediate certs are in the Trusted Root store..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking there are no intermediate certs are in the Trusted Root store" NDES_Validation 1
$IntermediateCertCheck = Get-Childitem cert:\LocalMachine\root -Recurse | Where-Object {$_.Issuer -ne $_.Subject}
if ($IntermediateCertCheck){
Write-Host "Error: Intermediate certificate found in the Trusted Root store. This can cause undesired effects and should be removed." -BackgroundColor red
Write-Host "Certificates:"
Write-Host
Write-Host $IntermediateCertCheck
Log-ScriptEvent $LogFilePath "Intermediate certificate found in the Trusted Root store: $($IntermediateCertCheck)" NDES_Validation 3
}
else {
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "Trusted Root store does not contain any Intermediate certificates."
Log-ScriptEvent $LogFilePath "Trusted Root store does not contain any Intermediate certificates." NDES_Validation 1
}
#endregion
#################################################################
#region Checking the EnrollmentAgentOffline and CEPEncryption are present
$ErrorActionPreference = "Silentlycontinue"
Write-host
Write-host "......................................................."
Write-host
Write-Host "Checking the EnrollmentAgentOffline and CEPEncryption are present..." -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking the EnrollmentAgentOffline and CEPEncryption are present" NDES_Validation 1
$certs = Get-ChildItem cert:\LocalMachine\My\
# Looping through all certificates in LocalMachine Store
Foreach ($item in $certs){
$Output = ($item.Extensions| where-object {$_.oid.FriendlyName -like "**"}).format(0).split(",")
if ($Output -match "EnrollmentAgentOffline"){
$EnrollmentAgentOffline = $TRUE
}
if ($Output -match "CEPEncryption"){
$CEPEncryption = $TRUE
}
}
# Checking if EnrollmentAgentOffline certificate is present
if ($EnrollmentAgentOffline){
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "EnrollmentAgentOffline certificate is present"
Log-ScriptEvent $LogFilePath "EnrollmentAgentOffline certificate is present" NDES_Validation 1
}
else {
Write-Host "Error: EnrollmentAgentOffline certificate is not present!" -BackgroundColor red
Write-Host "This can take place when an account without Enterprise Admin permissions installs NDES. You may need to remove the NDES role and reinstall with the correct permissions."
write-host 'Please review "Step 3.1 - Configure prerequisites on the NDES server".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "EnrollmentAgentOffline certificate is not present" NDES_Validation 3
}
# Checking if CEPEncryption is present
if ($CEPEncryption){
Write-Host "Success: " -ForegroundColor Green -NoNewline
Write-Host "CEPEncryption certificate is present"
Log-ScriptEvent $LogFilePath "CEPEncryption certificate is present" NDES_Validation 1
}
else {
Write-Host "Error: CEPEncryption certificate is not present!" -BackgroundColor red
Write-Host "This can take place when an account without Enterprise Admin permissions installs NDES. You may need to remove the NDES role and reinstall with the correct permissions."
write-host 'Please review "Step 3.1 - Configure prerequisites on the NDES server".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "CEPEncryption certificate is not present" NDES_Validation 3
}
$ErrorActionPreference = "Continue"
#endregion
#################################################################
#region Checking registry has been set with the SCEP certificate template name
Write-host
Write-host "......................................................."
Write-host
Write-Host 'Checking registry "HKLM:SOFTWARE\Microsoft\Cryptography\MSCEP" has been set with the SCEP certificate template name...' -ForegroundColor Yellow
Write-host
Log-ScriptEvent $LogFilePath "Checking registry (HKLM:SOFTWARE\Microsoft\Cryptography\MSCEP) has been set with the SCEP certificate template name" NDES_Validation 1
if (-not (Test-Path HKLM:SOFTWARE\Microsoft\Cryptography\MSCEP)){
Write-host "Error: Registry key does not exist. This can occur if the NDES role has been installed but not configured." -BackgroundColor Red
Write-host 'Please review "Step 3 - Configure prerequisites on the NDES server".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Log-ScriptEvent $LogFilePath "MSCEP Registry key does not exist." NDES_Validation 3
}
else {
$SignatureTemplate = (Get-ItemProperty -Path HKLM:SOFTWARE\Microsoft\Cryptography\MSCEP\ -Name SignatureTemplate).SignatureTemplate
$EncryptionTemplate = (Get-ItemProperty -Path HKLM:SOFTWARE\Microsoft\Cryptography\MSCEP\ -Name EncryptionTemplate).EncryptionTemplate
$GeneralPurposeTemplate = (Get-ItemProperty -Path HKLM:SOFTWARE\Microsoft\Cryptography\MSCEP\ -Name GeneralPurposeTemplate).GeneralPurposeTemplate
$DefaultUsageTemplate = "IPSECIntermediateOffline"
if ($SignatureTemplate -match $DefaultUsageTemplate -AND $EncryptionTemplate -match $DefaultUsageTemplate -AND $GeneralPurposeTemplate -match $DefaultUsageTemplate){
Write-Host "Error: Registry has not been configured with the SCEP Certificate template name. Default values have _not_ been changed." -BackgroundColor red
write-host 'Please review "Step 3.1 - Configure prerequisites on the NDES server".'
write-host "URL: https://docs.microsoft.com/en-us/intune/certificates-scep-configure#configure-your-infrastructure"
Write-Host
Log-ScriptEvent $LogFilePath "Registry has not been configured with the SCEP Certificate template name. Default values have _not_ been changed." NDES_Validation 3
$FurtherReading = $FALSE
}
else {
Write-Host "One or more default values have been changed."
Write-Host
write-host "Checking SignatureTemplate key..."
Write-host
if ($SignatureTemplate -match $SCEPUserCertTemplate){
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "SCEP certificate template '$($SCEPUserCertTemplate)' has been written to the registry under the _SignatureTemplate_ key. Ensure this aligns with the usage specificed on the SCEP template."
Write-host
Log-ScriptEvent $LogFilePath "SCEP certificate template $($SCEPUserCertTemplate)' has been written to the registry under the _SignatureTemplate_ key" NDES_Validation 1
}
else {
Write-Warning '"SignatureTemplate key does not match the SCEP certificate template name. Unless your template is explicitly set for the "Signature" purpose, this can safely be ignored."'
Write-Host
write-host "Registry value: " -NoNewline
Write-host "$($SignatureTemplate)" -ForegroundColor Cyan
Write-Host
write-host "SCEP certificate template value: " -NoNewline
Write-host "$($SCEPUserCertTemplate)" -ForegroundColor Cyan
Write-Host
Log-ScriptEvent $LogFilePath "SignatureTemplate key does not match the SCEP certificate template name.Registry value=$($SignatureTemplate)|SCEP certificate template value=$($SCEPUserCertTemplate)" NDES_Validation 2
}
Write-host "......................."
Write-Host
Write-Host "Checking EncryptionTemplate key..."
Write-host
if ($EncryptionTemplate -match $SCEPUserCertTemplate){
Write-Host "Success: " -ForegroundColor Green -NoNewline
write-host "SCEP certificate template '$($SCEPUserCertTemplate)' has been written to the registry under the _EncryptionTemplate_ key. Ensure this aligns with the usage specificed on the SCEP template."
Write-host
Log-ScriptEvent $LogFilePath "SCEP certificate template $($SCEPUserCertTemplate) has been written to the registry under the _EncryptionTemplate_ key" NDES_Validation 1
}
else {
Write-Warning '"EncryptionTemplate key does not match the SCEP certificate template name. Unless your template is explicitly set for the "Encryption" purpose, this can safely be ignored."'
Write-Host
write-host "Registry value: " -NoNewline
Write-host "$($EncryptionTemplate)" -ForegroundColor Cyan
Write-Host
write-host "SCEP certificate template value: " -NoNewline
Write-host "$($SCEPUserCertTemplate)" -ForegroundColor Cyan
Write-Host
Log-ScriptEvent $LogFilePath "EncryptionTemplate key does not match the SCEP certificate template name.Registry value=$($EncryptionTemplate)|SCEP certificate template value=$($SCEPUserCertTemplate)" NDES_Validation 2
}
Write-host "......................."
Write-Host
Write-Host "Checking GeneralPurposeTemplate key..."
Write-host
if ($GeneralPurposeTemplate -match $SCEPUserCertTemplate){