-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Instance.Tests.ps1
1335 lines (1282 loc) · 65.5 KB
/
Instance.Tests.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
$filename = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
. $PSScriptRoot/../internal/assertions/Instance.Assertions.ps1
# Check out the comments at the top of Instance.Assertions for guidance on adding checks
# Gather the instances we know are not contactable
[string[]]$NotContactable = (Get-PSFConfig -Module dbachecks -Name global.notcontactable).Value
# Get all the tags in use in this run
$Tags = Get-CheckInformation -Check $Check -Group Instance -AllChecks $AllChecks -ExcludeCheck $ChecksToExclude
@(Get-Instance).ForEach{
# Try to make a connection to the instance and add to NotContactable if required
if ($NotContactable -notcontains $psitem) {
$Instance = $psitem
try {
$InstanceSMO = Connect-DbaInstance -SqlInstance $Instance -ErrorAction SilentlyContinue -ErrorVariable errorvar
}
catch {
$NotContactable += $Instance
$There = $false
}
if ($NotContactable -notcontains $psitem) {
if ($null -eq $InstanceSMO.version) {
$NotContactable += $Instance
$There = $false
}
else {
$There = $True
}
}
}
else {
$There = $false
}
# Get the relevant information for the checks in one go to save repeated trips to the instance and set values for Not Contactable tests if required
$AllInstanceInfo = Get-AllInstanceInfo -Instance $InstanceSMO -Tags $Tags -There $There
Describe "Instance Connection" -Tags InstanceConnection, Connectivity, High, $filename {
$skipremote = Get-DbcConfigValue skip.connection.remoting
$skipping = Get-DbcConfigValue skip.connection.ping
$skipauth = Get-DbcConfigValue skip.connection.auth
$authscheme = Get-DbcConfigValue policy.connection.authscheme
if ($NotContactable -contains $psitem) {
Context "Testing Instance Connection on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Instance Connection on $psitem" {
It "connects successfully to $psitem" {
#Because Test-DbaInstance only shows connectsuccess false if the Connect-SQlInstance throws an error and we use Connect-DbaInstance
$true | Should -BeTrue
}
#local is always NTLM except when its a container ;-)
if ($InstanceSMO.NetBiosName -eq $ENV:COMPUTERNAME -and ($instance -notlike '*,*')) {
It -Skip:$skipauth "auth scheme should be NTLM on the local machine on $psitem" {
(Test-DbaConnectionAuthScheme -SqlInstance $Instance).authscheme | Should -Be NTLM
}
}
else {
It -Skip:$skipauth "auth scheme should be $authscheme on $psitem" {
(Test-DbaConnectionAuthScheme -SqlInstance $Instance).authscheme | Should -Be $authscheme
}
}
It -Skip:$skipping "We should be able to ping host $psitem" {
$ping = New-Object System.Net.NetworkInformation.Ping
$timeout = 1000 #milliseconds
$reply = $ping.Send($InstanceSMO.ComputerName, $timeout)
$pingable = $reply.Status -eq 'Success'
$pingable | Should -BeTrue
}
It -Skip:$skipremote "We should be able to remote onto $psitem" {
#simple remoting check
try {
$null = Invoke-Command -ComputerName $InstanceSMO.ComputerName -ScriptBlock { Get-ChildItem } -ErrorAction Stop
$remoting = $true
}
catch {
$remoting = $false
}
$remoting | Should -BeTrue
}
}
}
}
Describe "SQL Engine Service" -Tags SqlEngineServiceAccount, ServiceAccount, High, $filename {
$starttype = Get-DbcConfigValue policy.instance.sqlenginestart
$state = Get-DbcConfigValue policy.instance.sqlenginestate
if ($NotContactable -contains $psitem) {
Context "Testing SQL Engine Service on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
$IsClustered = $InstanceSMO.IsClustered
Context "Testing SQL Engine Service on $psitem" {
if ( -not $IsLInux) {
@(Get-DbaService -ComputerName $psitem -Type Engine -ErrorAction SilentlyContinue).ForEach{
It "SQL Engine service account should be $state on $($psitem.InstanceName)" {
Assert-EngineState -AllInstanceInfo $AllInstanceInfo -state $state
}
if ($IsClustered) {
It "SQL Engine service account should have a start mode of Manual on FailOver Clustered Instance $($psitem.InstanceName)" {
Assert-EngineStartTypeCluster -AllInstanceInfo $AllInstanceInfo
}
}
else {
It "SQL Engine service account should have a start mode of $starttype on standalone instance $($psitem.InstanceName)" {
Assert-EngineStartType -AllInstanceInfo $AllInstanceInfo -StartType $starttype
}
}
}
}
else {
It "Running on Linux so can't check Services on $Psitem" -skip {
}
}
}
}
}
Describe "TempDB Configuration" -Tags TempDbConfiguration, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing TempDB Configuration on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing TempDB Configuration on $psitem" {
$TempDBTest = Test-DbaTempdbConfig -SqlInstance $psitem
It "should have TF1118 enabled on $($TempDBTest[0].SqlInstance)" -Skip:((Get-DbcConfigValue skip.TempDb1118) -or ($InstanceSMO.VersionMajor -gt 12)) {
$TempDBTest[0].CurrentSetting | Should -Be $TempDBTest[0].Recommended -Because 'TF 1118 should be enabled'
}
It "should have $($TempDBTest[1].Recommended) TempDB Files on $($TempDBTest[1].SqlInstance)" -Skip:(Get-DbcConfigValue skip.tempdbfileCount) {
$TempDBTest[1].CurrentSetting | Should -Be $TempDBTest[1].Recommended -Because 'This is the recommended number of tempdb files for your server'
}
It "should not have TempDB Files autogrowth set to percent on $($TempDBTest[2].SqlInstance)" -Skip:(Get-DbcConfigValue skip.TempDbFileGrowthPercent) {
$TempDBTest[2].CurrentSetting | Should -Be $TempDBTest[2].Recommended -Because 'Auto growth type should not be percent'
}
It "should not have TempDB Files on the C Drive on $($TempDBTest[3].SqlInstance)" -Skip:(Get-DbcConfigValue skip.TempDbFilesonC) {
$TempDBTest[3].CurrentSetting | Should -Be $TempDBTest[3].Recommended -Because 'You do not want the tempdb files on the same drive as the operating system'
}
It "should not have TempDB Files with MaxSize Set on $($TempDBTest[4].SqlInstance)" -Skip:(Get-DbcConfigValue skip.TempDbFileSizeMax) {
$TempDBTest[4].CurrentSetting | Should -Be $TempDBTest[4].Recommended -Because 'Tempdb files should be able to grow'
}
It "The data files should all be the same size on $($TempDBTest[0].SqlInstance)" {
Assert-TempDBSize -Instance $Psitem
}
}
}
}
Describe "Ad Hoc Workload Optimization" -Tags AdHocWorkload, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing Ad Hoc Workload Optimization on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Ad Hoc Workload Optimization on $psitem" {
It "Should have Optimize for Ad Hoc workloads set correctly on $psitem" -Skip:((Get-Version -SQLInstance $psitem) -lt 10) {
@(Test-DbaOptimizeForAdHoc -SqlInstance $psitem).ForEach{
$psitem.CurrentOptimizeAdHoc | Should -Be $psitem.RecommendedOptimizeAdHoc -Because "optimize for ad hoc workloads is a recommended setting"
}
}
}
}
}
Describe "Backup Path Access" -Tags BackupPathAccess, Storage, DISA, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing Backup Path Access on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Backup Path Access on $psitem" {
$backuppath = Get-DbcConfigValue policy.storage.backuppath
if (-not $backuppath) {
$backuppath = (Get-DbaDefaultPath -SqlInstance $psitem).Backup
}
It "can access backup path ($backuppath) on $psitem" {
Test-DbaPath -SqlInstance $psitem -Path $backuppath | Should -BeTrue -Because 'The SQL Service account needs to have access to the backup path to backup your databases'
}
}
}
}
Describe "Default File Path" -Tags DefaultFilePath, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing Default File Path on $psitem" {
It "Can't Connect to $Psitem" {
$false | Should -BeTrue -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Default Data File Path on $psitem" {
It "Default Data File Path on $psitem" {
$diskFile = Get-DbaInstanceProperty -SqlInstance $psitem | Where-Object Name -eq DefaultFile
$diskFile.Value.substring(0, 1) | Should -Not -Be "C" -Because 'Default Data file path should not be your C:\ drive'
}
}
Context "Testing Default Log File Path on $psitem" {
It "Default Log File Path on $psitem" {
$diskLog = Get-DbaInstanceProperty -SqlInstance $psitem | Where-Object Name -eq DefaultLog
$diskLog.Value.substring(0, 1) | Should -Not -Be "C" -Because 'Default Log file path should not be your C:\ drive'
}
}
}
}
Describe "Dedicated Administrator Connection" -Tags DAC, CIS, Low, $filename {
$dac = Get-DbcConfigValue policy.dacallowed
if ($NotContactable -contains $psitem) {
Context "Testing Dedicated Administrator Connection on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Dedicated Administrator Connection on $psitem" {
It "DAC is set to $dac on $psitem" {
(Get-DbaSpConfigure -SqlInstance $psitem -ConfigName 'RemoteDACConnectionsEnabled').ConfiguredValue -eq 1 | Should -Be $dac -Because 'This is the setting that you have chosen for DAC connections'
}
}
}
}
Describe "Network Latency" -Tags NetworkLatency, Connectivity, Medium, $filename {
$max = Get-DbcConfigValue policy.network.latencymaxms
if ($NotContactable -contains $psitem) {
Context "Testing Network Latency on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Network Latency on $psitem" {
@(Test-DbaNetworkLatency -SqlInstance $psitem).ForEach{
It "network latency should be less than $max ms on $($psitem.SqlInstance)" {
$psitem.Average.TotalMilliseconds | Should -BeLessThan $max -Because 'You do not want to be waiting on the network'
}
}
}
}
}
Describe "Linked Servers" -Tags LinkedServerConnection, Connectivity, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing Linked Servers on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Linked Servers on $psitem" {
@(Test-DbaLinkedServerConnection -SqlInstance $psitem).ForEach{
It "Linked Server $($psitem.LinkedServerName) has connectivity on $($psitem.SqlInstance)" {
$psitem.Connectivity | Should -BeTrue -Because 'You need to be able to connect to your linked servers'
}
}
}
}
}
Describe "Max Memory" -Tags MaxMemory, High, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing Max Memory on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Max Memory on $psitem" {
if (-not $IsLInux) {
It "Max Memory setting should be correct on $psitem" {
@(Test-DbaMaxMemory -SqlInstance $psitem).ForEach{
$psitem.SqlMaxMB | Should -BeLessThan ($psitem.RecommendedMB + 379) -Because 'You do not want to exhaust server memory'
}
}
}
else {
It "Max Memory setting should be correct (running on Linux so only checking Max Memory is less than Total Memory) on $psitem" {
# simply check that the max memory is less than total memory
$MemoryValues = Get-DbaMaxMemory -SqlInstance $psitem
$MemoryValues.Total | Should -BeGreaterThan $MemoryValues.MaxValue -Because 'You do not want to exhaust server memory'
}
}
}
}
}
Describe "Orphaned Files" -Tags OrphanedFile, Low, $filename {
if ($NotContactable -contains $psitem) {
Context "Checking for orphaned database files on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking for orphaned database files on $psitem" {
It "There should be zero orphaned database files on $psitem" {
@(Find-DbaOrphanedFile -SqlInstance $psitem).Count | Should -Be 0 -Because 'You dont want any orphaned files - Use Find-DbaOrphanedFile to locate them'
}
}
}
}
Describe "SQL and Windows names match" -Tags ServerNameMatch, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing instance name matches Windows name for $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing instance name matches Windows name for $psitem" {
if ($InstanceSMO.NetBiosName -eq $ENV:COMPUTERNAME -and ($instance -like '*,*')) {
It "We wont check this as it appears to be a local container - for $psitem" -Skip {
}
}
else {
It "Testing rename required for $psitem" {
(Test-DbaInstanceName -SqlInstance $psitem).RenameRequired | Should -BeFalse -Because 'SQL and Windows should agree on the server name'
}
}
}
}
}
Describe "SQL Memory Dumps" -Tags MemoryDump, Medium, $filename {
$maxdumps = Get-DbcConfigValue policy.dump.maxcount
if ($NotContactable -contains $psitem) {
Context "Checking that dumps on $psitem do not exceed $maxdumps for $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that dumps on $psitem do not exceed $maxdumps for $psitem" {
It "dump count of $count is less than or equal to the $maxdumps dumps on $psitem" -Skip:($InstanceSMO.Version.Major -lt 10 ) {
Assert-MaxDump -AllInstanceInfo $AllInstanceInfo -maxdumps $maxdumps
}
}
}
}
Describe "Supported Build" -Tags SupportedBuild, DISA, High, $filename {
$BuildWarning = Get-DbcConfigValue policy.build.warningwindow
$BuildBehind = Get-DbcConfigValue policy.build.behind
$Date = Get-Date
if ($NotContactable -contains $psitem) {
Context "Checking that build is still supported by Microsoft for $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that build is still supported by Microsoft for $psitem" {
if ($BuildBehind) {
It "The build is not behind the latest build by more than $BuildBehind for $psitem" {
Assert-InstanceSupportedBuild -Instance $InstanceSMO -BuildBehind $BuildBehind -Date $Date
}
}
It "The build is supported by Microsoft for $psitem" {
Assert-InstanceSupportedBuild -Instance $InstanceSMO -Date $Date
}
It "The build is supported by Microsoft within the warning window of $BuildWarning months for $psitem" {
Assert-InstanceSupportedBuild -Instance $InstanceSMO -BuildWarning $BuildWarning -Date $Date
}
}
}
}
Describe "SA Login Renamed" -Tags SaRenamed, DISA, CIS, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Checking that sa login has been renamed on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that sa login has been renamed on $psitem" {
$results = Get-DbaLogin -SqlInstance $psitem -Login sa
It "sa login has been renamed on $psitem" {
$results | Should -Be $null -Because 'Renaming the sa account is a requirement'
}
}
}
}
Describe "SA Login Disabled" -Tags SaDisabled, DISA, CIS, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Checking that sa login has been disabled on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that sa login has been disabled on $psitem" {
$skip = Get-DbcConfigValue skip.security.sadisabled
It "sa login is disabled on $psitem" -Skip:$Skip {
Assert-SaDisabled -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Login SA cannot exist" -Tags SaExist, CIS, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Checking that a login named sa does not exist on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that a login named sa does not exist on $psitem" {
$skip = Get-DbcConfigValue skip.security.saexist
It "sa login does not exist on $psitem" -Skip:$Skip {
Assert-SaExist -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Default Backup Compression" -Tags DefaultBackupCompression, Low, $filename {
$defaultbackupcompression = Get-DbcConfigValue policy.backup.defaultbackupcompression
if ($NotContactable -contains $psitem) {
Context "Testing Default Backup Compression on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Default Backup Compression on $psitem" {
It "Default Backup Compression is set to $defaultbackupcompression on $psitem" -Skip:((Get-Version -SQLInstance $psitem) -lt 10) {
Assert-BackupCompression -Instance $psitem -defaultbackupcompression $defaultbackupcompression
}
}
}
}
Describe "XE Sessions That should be Stopped" -Tags XESessionStopped, ExtendedEvent, Medium, $filename {
$xesession = Get-DbcConfigValue policy.xevent.requiredstoppedsession
if ((Get-Version -SQLInstance $psitem) -gt 10) {
# no point running if we dont have something to check
if ($xesession) {
if ($NotContactable -contains $psitem) {
Context "Checking sessions on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking sessions on $psitem" {
$runningsessions = (Get-DbaXESession -SqlInstance $psitem).Where{ $_.Status -eq 'Running' }.Name
@($xesession).ForEach{
It "Session $psitem should not be running on $Instance" {
$psitem | Should -Not -BeIn $runningsessions -Because "$psitem session should be stopped"
}
}
}
}
}
else {
Write-Warning "You need to use Set-DbcConfig -Name policy.xevent.requiredstoppedsession -Value to add some Extended Events session names to run this check"
}
}
else {
Context "Checking sessions on $psitem" {
It "Version does not support XE sessions on $Instance" -skip {
1 | Should -Be 3
}
}
}
}
Describe "XE Sessions That should be Running" -Tags XESessionRunning, ExtendedEvent, Medium, $filename {
$xesession = Get-DbcConfigValue policy.xevent.requiredrunningsession
if ((Get-Version -SQLInstance $psitem) -gt 10) {
# no point running if we dont have something to check
if ($xesession) {
if ($NotContactable -contains $psitem) {
Context "Checking running sessions on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking running sessions on $psitem" {
$runningsessions = (Get-DbaXESession -SqlInstance $psitem).Where{ $_.Status -eq 'Running' }.Name
@($xesession).ForEach{
It "session $psitem should be running on $Instance" {
$psitem | Should -BeIn $runningsessions -Because "$psitem session should be running"
}
}
}
}
}
else {
Write-Warning "You need to use Set-DbcConfig -Name policy.xevent.requiredrunningsession -Value to add some Extended Events session names to run this check"
}
}
else {
Context "Checking running sessions on $psitem" {
It "Version does not support XE sessions on $Instance" -skip {
1 | Should -Be 3
}
}
}
}
Describe "XE Sessions That Are Allowed to Be Running" -Tags XESessionRunningAllowed, ExtendedEvent, Medium, $filename {
$xesession = Get-DbcConfigValue policy.xevent.validrunningsession
if ((Get-Version -SQLInstance $psitem) -gt 10) {
# no point running if we dont have something to check
if ($xesession) {
if ($NotContactable -contains $psitem) {
Context "Checking running sessions allowed on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking running sessions allowed on $psitem" {
@(Get-DbaXESession -SqlInstance $psitem).Where{ $_.Status -eq 'Running' }.ForEach{
It "Session $($Psitem.Name) is allowed to be running on $Instance" {
$psitem.name | Should -BeIn $xesession -Because "Only these sessions are allowed to be running"
}
}
}
}
}
else {
Write-Warning "You need to use Set-DbcConfig -Name policy.xevent.validrunningsession -Value to add some Extended Events session names to run this check"
}
}
else {
Context "Checking running sessions allowed on $psitem" {
It "Version does not support XE sessions on $Instance" -skip {
1 | Should -Be 3
}
}
}
}
Describe "OLE Automation" -Tags OLEAutomation, security, CIS, Medium, $filename {
$OLEAutomation = Get-DbcConfigValue policy.oleautomation
if ($NotContactable -contains $psitem) {
Context "Testing OLE Automation on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing OLE Automation on $psitem" {
It "OLE Automation is set to $OLEAutomation on $psitem" {
(Get-DbaSpConfigure -SqlInstance $psitem -ConfigName 'OleAutomationProceduresEnabled').ConfiguredValue -eq 1 | Should -Be $OLEAutomation -Because 'OLE Automation can introduce additional security risks'
}
}
}
}
Describe "sp_whoisactive is Installed" -Tags WhoIsActiveInstalled, Low, $filename {
$db = Get-DbcConfigValue policy.whoisactive.database
if ($NotContactable -contains $psitem) {
Context "Testing WhoIsActive exists on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing WhoIsActive exists on $psitem" {
It "WhoIsActive should exists on $db on $psitem" {
(Get-DbaModule -SqlInstance $psitem -Database $db -Type StoredProcedure | Where-Object name -eq "sp_WhoIsActive") | Should -Not -Be $Null -Because 'The sp_WhoIsActive stored procedure should be installed'
}
}
}
}
Describe "Model Database Growth" -Tags ModelDbGrowth, Low, $filename {
$modeldbgrowthtest = Get-DbcConfigValue skip.instance.modeldbgrowth
if (-not $modeldbgrowthtest) {
if ($NotContactable -contains $psitem) {
Context "Testing model database growth setting is not default on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing model database growth setting is not default on $psitem" {
@(Get-DbaDbFile -SqlInstance $psitem -Database Model).ForEach{
It "Model database growth settings should not be percent for file $($psitem.LogicalName) on $($psitem.SqlInstance)" {
$psitem.GrowthType | Should -Not -Be 'Percent' -Because 'New databases use the model database as a template and percent growth can cause performance problems'
}
It "Model database growth settings should not be 1Mb for file $($psitem.LogicalName) on $($psitem.SqlInstance)" {
$psitem.Growth | Should -Not -Be 1024 -Because 'New databases use the model database as a template and growing for each Mb will have a performance impact'
}
}
}
}
}
}
Describe "Ad Users and Groups " -Tags ADUser, Domain, High, $filename {
if (-not $IsLinux) {
$userexclude = Get-DbcConfigValue policy.adloginuser.excludecheck
$groupexclude = Get-DbcConfigValue policy.adlogingroup.excludecheck
if ($NotContactable -contains $psitem) {
Context "Testing Active Directory users on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
Context "Testing Active Directory groups on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Active Directory users on $psitem" {
@(Test-DbaWindowsLogin -SqlInstance $psitem -FilterBy LoginsOnly -ExcludeLogin $userexclude).ForEach{
It "Active Directory user $($psitem.login) was found in $Instance on $($psitem.domain)" {
$psitem.found | Should -Be $true -Because "$($psitem.login) should be in Active Directory"
}
if ($psitem.found -eq $true) {
It "Active Directory user $($psitem.login) should not have an expired password in $Instance on $($psitem.domain)" {
$psitem.PasswordExpired | Should -Be $false -Because "$($psitem.login) password should not be expired"
}
It "Active Directory user $($psitem.login) should not be locked out in $Instance on $($psitem.domain)" {
$psitem.lockedout | Should -Be $false -Because "$($psitem.login) should not be locked out"
}
It "Active Directory user $($psitem.login) should be enabled in $Instance on $($psitem.domain)" {
$psitem.Enabled | Should -Be $true -Because "$($psitem.login) should be enabled"
}
It "Active Directory user $($psitem.login) should not be disabled in $Instance on $($psitem.Server)" {
$psitem.DisabledInSQLServer | Should -Be $false -Because "$($psitem.login) should be active on the SQL server"
}
}
}
}
Context "Testing Active Directory groups on $psitem" {
@(Test-DbaWindowsLogin -SqlInstance $psitem -FilterBy GroupsOnly -ExcludeLogin $groupexclude).ForEach{
It "Active Directory group $($psitem.login) was found in $Instance on $($psitem.domain)" {
$psitem.found | Should -Be $true -Because "$($psitem.login) should be in Active Directory"
}
if ($psitem.found -eq $true) {
It "Active Directory group $($psitem.login) should not be disabled in $Instance on $($psitem.Server)" {
$psitem.DisabledInSQLServer | Should -Be $false -Because "$($psitem.login) should be active on the SQL server"
}
}
}
}
}
}
else {
Context "Testing Active Directory users on $psitem" {
It "Running on Linux so can't check AD on $Psitem" -skip {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
Context "Testing Active Directory groups on $psitem" {
It "Running on Linux so can't check AD on $Psitem" -skip {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
}
Describe "Error Log Entries" -Tags ErrorLog, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Checking error log on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking error log on $psitem" {
It "Error log should be free of error severities 17-24 on $psitem" {
Assert-ErrorLogEntry -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Error Log Count" -Tags ErrorLogCount, CIS, Low, $filename {
$errorLogCount = Get-DbcConfigValue policy.errorlog.logcount
if ($NotContactable -contains $psitem) {
Context "Checking error log count on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking error log count on $psitem" {
It "Error log count should be greater or equal to $errorLogCount on $psitem" {
Assert-ErrorLogCount -SqlInstance $psitem -errorLogCount $errorLogCount
}
}
}
}
Describe "Instance MaxDop" -Tags MaxDopInstance, MaxDop, Medium, $filename {
$UseRecommended = Get-DbcConfigValue policy.instancemaxdop.userecommended
$MaxDop = Get-DbcConfigValue policy.instancemaxdop.maxdop
$ExcludeInstance = Get-DbcConfigValue policy.instancemaxdop.excludeinstance
if ($NotContactable -contains $psitem) {
Context "Testing Instance MaxDop Value on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
if ($psitem -in $ExcludeInstance) { $Skip = $true }else { $skip = $false }
Context "Testing Instance MaxDop Value on $psitem" {
It "Instance Level MaxDop setting should be correct on $psitem" -Skip:$Skip {
Assert-InstanceMaxDop -Instance $psitem -UseRecommended:$UseRecommended -MaxDopValue $MaxDop
}
}
}
}
Describe "Two Digit Year Cutoff" -Tags TwoDigitYearCutoff, Low, $filename {
$twodigityearcutoff = Get-DbcConfigValue policy.twodigityearcutoff
if ($NotContactable -contains $psitem) {
Context "Testing Two Digit Year Cutoff on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Two Digit Year Cutoff on $psitem" {
It "Two Digit Year Cutoff is set to $twodigityearcutoff on $psitem" {
Assert-TwoDigitYearCutoff -Instance $psitem -TwoDigitYearCutoff $twodigityearcutoff
}
}
}
}
Describe "Trace Flags Expected" -Tags TraceFlagsExpected, TraceFlag, High, $filename {
$ExpectedTraceFlags = Get-DbcConfigValue policy.traceflags.expected
if ($NotContactable -contains $psitem) {
Context "Testing Expected Trace Flags on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Expected Trace Flags on $psitem" {
It "Expected Trace Flags $ExpectedTraceFlags exist on $psitem" {
Assert-TraceFlag -SQLInstance $psitem -ExpectedTraceFlag $ExpectedTraceFlags
}
}
}
}
Describe "Trace Flags Not Expected" -Tags TraceFlagsNotExpected, TraceFlag, Medium, $filename {
$NotExpectedTraceFlags = Get-DbcConfigValue policy.traceflags.notexpected
$ExpectedTraceFlags = Get-DbcConfigValue policy.traceflags.expected
if ($NotContactable -contains $psitem) {
Context "Testing Not Expected Trace Flags on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Not Expected Trace Flags on $psitem" {
It "Expected Trace Flags $NotExpectedTraceFlags to not exist on $psitem" {
Assert-NotTraceFlag -SQLInstance $psitem -NotExpectedTraceFlag $NotExpectedTraceFlags -ExpectedTraceFlag $ExpectedTraceFlags
}
}
}
}
Describe "CLR Enabled" -Tags CLREnabled, security, CIS, High, $filename {
$CLREnabled = Get-DbcConfigValue policy.security.clrenabled
if ($NotContactable -contains $psitem) {
Context "Testing CLR Enabled on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing CLR Enabled on $psitem" {
It "CLR Enabled is set to $CLREnabled on $psitem" {
Assert-CLREnabled -SQLInstance $psitem -CLREnabled $CLREnabled
}
}
}
}
Describe "Cross Database Ownership Chaining" -Tags CrossDBOwnershipChaining, security, CIS, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Testing Cross Database Ownership Chaining on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Cross Database Ownership Chaining on $psitem" {
It "Cross Database Ownership Chaining should be disabled on $psitem" {
Assert-CrossDBOwnershipChaining -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Ad Hoc Distributed Queries" -Tags AdHocDistributedQueriesEnabled, security, CIS, Medium, $filename {
$AdHocDistributedQueriesEnabled = Get-DbcConfigValue policy.security.AdHocDistributedQueriesEnabled
if ($NotContactable -contains $psitem) {
Context "Testing Ad Hoc Distributed Queries on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Ad Hoc Distributed Queries on $psitem" {
It "Ad Hoc Distributed Queries is set to $AdHocDistributedQueriesEnabled on $psitem" {
Assert-AdHocDistributedQueriesEnabled -SQLInstance $Psitem -AdHocDistributedQueriesEnabled $AdHocDistributedQueriesEnabled
}
}
}
}
Describe "XP CmdShell" -Tags XpCmdShellDisabled, security, CIS, Medium, $filename {
$XpCmdShellDisabled = Get-DbcConfigValue policy.security.XpCmdShellDisabled
if ($NotContactable -contains $psitem) {
Context "Testing XP CmdShell on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing XP CmdShell on $psitem" {
It "XPCmdShell is set to $XpCmdShellDisabled on $psitem" {
Assert-XpCmdShellDisabled -SQLInstance $Psitem -XpCmdShellDisabled $XpCmdShellDisabled
}
}
}
}
Describe "Scan For Startup Procedures" -Tags ScanForStartupProceduresDisabled, Security, CIS, Medium, $filename {
$skip = Get-DbcConfigValue skip.instance.scanforstartupproceduresdisabled
if ($NotContactable -contains $psitem) {
Context "Testing Scan For Startup Procedures on $psitem" {
It "Can't Connect to $Psitem" -Skip:$skip {
$false | Should -BeTrue -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Scan For Startup Procedures on $psitem" {
It "Scan For Startup Procedures should be disabled on $psitem" -Skip:$skip {
Assert-ScanForStartupProcedures -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Default Trace" -Tags DefaultTrace, CIS, Low, $filename {
$skip = Get-DbcConfigValue skip.instance.defaulttrace
if ($NotContactable -contains $psitem) {
Context "Checking Default Trace on $psitem" {
It "Can't Connect to $Psitem" -Skip:$skip {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking Default Trace on $psitem" {
It "The Default Trace should be enabled on $psitem" -Skip:$skip {
Assert-DefaultTrace -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "OLE Automation Procedures Disabled" -Tags OLEAutomationProceduresDisabled, CIS, Low, $filename {
$skip = Get-DbcConfigValue skip.instance.oleautomationproceduresdisabled
if ($NotContactable -contains $psitem) {
Context "Checking OLE Automation Procedures on $psitem" {
It "Can't Connect to $Psitem" -Skip:$skip {
$false | Should -BeTrue -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking OLE Automation Procedures on $psitem" {
It "The OLE Automation Procedures should be disabled on $psitem" -Skip:$skip {
Assert-OLEAutomationProcedures -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Remote Access Disabled" -Tags RemoteAccessDisabled, Security, CIS, Medium, $filename {
$skip = Get-DbcConfigValue skip.instance.remoteaccessdisabled
if ($NotContactable -contains $psitem) {
Context "Testing Remote Access on $psitem" {
It "Can't Connect to $Psitem" -Skip:$skip {
$false | Should -BeTrue -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Remote Access on $psitem" {
It "The Remote Access should be disabled on $psitem" -Skip:$skip {
Assert-RemoteAccess -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Latest Build" -Tags LatestBuild, Security, CIS, Medium, $filename {
$skip = Get-DbcConfigValue skip.instance.latestbuild
if ($NotContactable -contains $psitem) {
Context "Testing Latest Build on $psitem" {
It "Can't Connect to $Psitem" -Skip:$skip {
$false | Should -BeTrue -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Testing Latest Build on $psitem" {
It "The Latest Build of SQL should be installed on $psitem" -Skip:$skip {
Assert-LatestBuild -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Login BUILTIN Administrators cannot exist" -Tags BuiltInAdmin, CIS, Medium, $filename {
if ($NotContactable -contains $psitem) {
Context "Checking that a login named BUILTIN\Administrators does not exist on $psitem" {
It "Can't Connect to $Psitem" {
$true | Should -BeFalse -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that a login named BUILTIN\Administrators does not exist on $psitem" {
$skip = Get-DbcConfigValue skip.security.builtinadmin
It "BUILTIN\Administrators login does not exist on $psitem" -Skip:$skip {
Assert-BuiltInAdmin -AllInstanceInfo $AllInstanceInfo
}
}
}
}
Describe "Local Windows Groups Not Have SQL Logins" -Tags LocalWindowsGroup, Security, CIS, Medium, $filename {
$skip = Get-DbcConfigValue skip.security.localwindowsgroup
if ($NotContactable -contains $psitem) {
Context "Checking that local Windows groups do not have SQL Logins on $psitem" {
It "Can't Connect to $Psitem" -Skip:$skip {
$false | Should -BeTrue -Because "The instance should be available to be connected to!"
}
}
}
else {
Context "Checking that local Windows groups do not have SQL Logins on $psitem" {
It "Local Windows groups should not SQL Logins on $psitem" -Skip:$skip {
Assert-LocalWindowsGroup -AllInstanceInfo $AllInstanceInfo