23
23
This will run Pester tests against SQLServer1 instance and check using all the variables
24
24
. EXAMPLE
25
25
$Parms = @{
26
- Servers = 'SQLServer1','SQLServer2','SQLServer3';
26
+ Servers = 'SQLServer1','SQLServer2','SQLServer2\Instance1',' SQLServer3';
27
27
SQLAdmins = 'THEBEARD\Rob','THEBEARD\SQLAdmins';
28
28
BackupDirectory = 'C:\MSSQL\Backup';
29
29
DataDirectory = 'C:\MSSQL\Data\';
57
57
58
58
Test-SQLDefault @Parms
59
59
60
- This example uses splatting to hold the parameters and will run the tests against SQLServer1, SQLServer2 and SQLServer3
60
+ This example uses splatting to hold the parameters and will run the tests against SQLServer1, SQLServer2, SQLServer2\Instance1 and SQLServer3
61
61
. NOTES
62
62
AUTHOR : Rob Sewell http://sqldbawithabeard.com
63
63
Initial 12/05/2016
64
64
#>
65
65
function Test-SQLDefault {
66
66
[CmdletBinding ()]
67
67
param (
68
- # Server Name or an array of server names
68
+ # Server Name or ServerName\InstanceName or an array of server names and/or servername\instancenames
69
69
[Parameter (Mandatory = $true ,
70
70
ValueFromPipeline = $true ,
71
71
ValueFromPipelineByPropertyName = $true ,
72
72
Position = 0 )]
73
- [ string ] $Servers ,
73
+ $Servers ,
74
74
# Expected SQL Admin Account or an array of accounts
75
75
[Parameter (Mandatory = $true ,
76
76
ValueFromPipeline = $true ,
@@ -264,13 +264,29 @@ param(
264
264
[string ]$LogSPBlitzToTableStartTime
265
265
)
266
266
foreach ($Server in $Servers )
267
- {
267
+
268
+ {
269
+ if ($Server.Contains (' \' ))
270
+ {
271
+ $ServerName = $Server.Split (' \' )[0 ]
272
+ }
273
+ else
274
+ {
275
+ $Servername = $Server
276
+ }
277
+
278
+
268
279
# # Check for connectivity
269
- if ((Test-Connection $Server - count 1 - Quiet) -eq $false ){
270
- Write-Error ' Could not connect to $Server '
280
+ if ((Test-Connection $ServerName - count 1 - Quiet) -eq $false ){
281
+ Write-Error " Could not connect to $ServerName "
271
282
$_
272
283
continue
273
284
}
285
+ if ([bool ](Test-WSMan - ComputerName $ServerName - ErrorAction SilentlyContinue))
286
+ {}
287
+ else
288
+ {Write-Error " PSRemoting is not enabled on $ServerName Please enable and retry"
289
+ continue }
274
290
275
291
Describe " $Server " {
276
292
BeforeAll {
@@ -299,7 +315,7 @@ foreach($Server in $Servers)
299
315
$Return.RestoreProc = $Sps -contains ' RestoreCommand'
300
316
$Return.OlaSysFullEnabled = $srv.JobServer.jobs [' DatabaseBackup - SYSTEM_DATABASES - FULL' ].IsEnabled
301
317
$Return.OlaSysFullScheduled = $srv.JobServer.jobs [' DatabaseBackup - SYSTEM_DATABASES - FULL' ].HasSchedule
302
- $Return.OlaSysFullFrequency = $srv.JobServer.jobs [' DatabaseBackup - SYSTEM_DATABASES - FULL' ].JobSchedules.FrequencyTypes.value__
318
+ $Return.OlaSysFullFrequency = $srv.JobServer.jobs [' DatabaseBackup - SYSTEM_DATABASES - FULL' ].JobSchedules.FrequencyTypes
303
319
$Return.OlaSysFullStartTime = $srv.JobServer.jobs [' DatabaseBackup - SYSTEM_DATABASES - FULL' ].JobSchedules.ActiveStartTimeOfDay
304
320
$Return.OlaUserFullEnabled = $srv.JobServer.jobs [' DatabaseBackup - USER_DATABASES - FULL' ].IsEnabled
305
321
$Return.OlaUserFullScheduled = $srv.JobServer.jobs [' DatabaseBackup - USER_DATABASES - FULL' ].HasSchedule
@@ -337,23 +353,17 @@ foreach($Server in $Servers)
337
353
$Return.SysDatabasesFullBackupToday = $srv.Databases.Where {$_.IsSystemObject -eq $true -and $_.Name -ne ' tempdb' -and $_.LastBackupDate -lt (Get-Date ).AddDays(-1 )}.Count
338
354
Return $Return
339
355
}
340
- try {
341
- $Return = Invoke-Command - ScriptBlock $Scriptblock - ComputerName $Server - ErrorAction Stop
342
- }
343
- catch {
344
- Write-Error " Unable to Connect to $Server "
345
- $Error
346
- continue
347
- }
356
+ $Return = Invoke-Command - ScriptBlock $Scriptblock - ComputerName $ServerName
348
357
}
349
358
Context ' Server' {
350
359
It ' Should Exist and respond to ping' {
351
- $connect = Test-Connection $Server - count 1 - Quiet
360
+ $connect = Test-Connection $ServerName - count 1 - Quiet
352
361
$Connect | Should Be $true
353
362
}
363
+ if ($connect -eq $false ){break }
354
364
It ' Should have SQL Server Installed' {
355
365
$Scriptblock = {(Get-ChildItem ' HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQLServer' - ErrorAction SilentlyContinue)}
356
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
366
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
357
367
$State | Should Be $true
358
368
}
359
369
} # End Context
@@ -362,54 +372,55 @@ foreach($Server in $Servers)
362
372
(Get-Service - ComputerName $Server - Name MSSQLServer).Status | Should Be ' Running'
363
373
}
364
374
It ' SQL Db Engine should be Automatic Start' {
365
- (Get-CimInstance - ClassName Win32_Service - Filter " Name = 'MSSQLServer'" - ComputerName $Server ).StartMode | should be ' Auto'
375
+ (Get-CimInstance - ClassName Win32_Service - Filter " Name = 'MSSQLServer'" - ComputerName $ServerName ).StartMode | should be ' Auto'
366
376
}
367
377
It ' SQL Agent should be running' {
368
- (Get-Service - ComputerName $Server - Name SQLServerAgent).Status | Should Be ' Running'
378
+ (Get-Service - ComputerName $ServerName - Name SQLServerAgent).Status | Should Be ' Running'
369
379
}
370
380
It ' SQL Agent should be Automatic Start' {
371
- (Get-CimInstance - ClassName Win32_Service - Filter " Name = 'SQLServerAgent'" - ComputerName $Server ).StartMode | should be ' Auto'
381
+ (Get-CimInstance - ClassName Win32_Service - Filter " Name = 'SQLServerAgent'" - ComputerName $ServerName ).StartMode | should be ' Auto'
372
382
}
373
383
} # End Context
374
- Context ' FireWall' {
384
+ Context ' FireWall' {
385
+
375
386
It ' Should have a Firewall connection for SQL Browser' {
376
387
$Scriptblock = {Get-NetFirewallRule - Name ' SQL Browser Service - Allow' }
377
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
378
- $State | Should Be $true
388
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
389
+ $State | Should Be $true
379
390
}
380
391
It ' Firewall connection for SQL Browser should be enabled' {
381
392
$Scriptblock = {(Get-NetFirewallRule - Name ' SQL Browser Service - Allow' ).Enabled}
382
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
393
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
383
394
$State | Should Be $true
384
395
}
385
396
It ' SQL Browser Firewall Action Should Be Allow' {
386
397
$Scriptblock = {(Get-NetFirewallRule - Name ' SQL Browser Service - Allow' ).Action}
387
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
398
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
388
399
$State.value | Should Be ' Allow'
389
400
}
390
401
It ' SQL Browser Firewall Application should be the SQLBrowser.exe' {
391
402
$Scriptblock = {(Get-NetFirewallRule - Name ' SQL Browser Service - Allow' | Get-NetFirewallApplicationFilter ).Program}
392
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
403
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
393
404
$State | Should Be ' C:\Program Files (x86)\Microsoft SQL Server\90\Shared\sqlbrowser.exe'
394
405
}
395
406
It ' Should have a Firewall connection for SQL DB Engine' {
396
407
$Scriptblock = {Get-NetFirewallRule - Name ' SQL Database Engine - Allow' }
397
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
408
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
398
409
$State | Should Be $true
399
410
}
400
411
It ' Firewall connection for SQL DB Engine should be enabled' {
401
412
$Scriptblock = {(Get-NetFirewallRule - Name ' SQL Database Engine - Allow' ).Enabled}
402
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
413
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
403
414
$State | Should Be $true
404
415
}
405
416
It ' DB EngineFirewall Action Should Be Allow' {
406
417
$Scriptblock = {(Get-NetFirewallRule - Name ' SQL Database Engine - Allow' ).Action}
407
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
418
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
408
419
$State.value | Should Be ' Allow'
409
420
}
410
421
It ' DB EngineFirewall Application should be the SQLBrowaser.exe' {
411
422
$Scriptblock = {(Get-NetFirewallRule - Name ' SQL Database Engine - Allow' | Get-NetFirewallApplicationFilter ).Program}
412
- $State = Invoke-Command - ComputerName $Server - ScriptBlock $Scriptblock
423
+ $State = Invoke-Command - ComputerName $ServerName - ScriptBlock $Scriptblock
413
424
$State | Should Be ' C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\sqlservr.exe'
414
425
}
415
426
} # End Context Firewall
@@ -420,7 +431,7 @@ foreach($Server in $Servers)
420
431
It ' Databases should have a normal Status - No Restoring, Recovery Pending etc' {
421
432
$Return.DatabasesStatus | Should Be 0
422
433
}
423
- It ' System Databases Should have been backed up within the last 24 hours' {
434
+ It ' System Databases Shol dhave been backed up within the last 24 hours' {
424
435
$Return.SysDatabasesFullBackupToday | SHould be 0
425
436
}
426
437
} # End Context
@@ -460,9 +471,6 @@ foreach($Server in $Servers)
460
471
It ' Should have alerts for 823,824 and 825' {
461
472
$Return.Alerts82345Exist | Should Be 3
462
473
}
463
- It ' Alerts for 823,824 and 825 should be enebled' {
464
- $Return.Alerts82345Enabled | Should Be 3
465
- }
466
474
} # End Context
467
475
Context ' Agent Jobs' {
468
476
It ' Should have Agent Jobs' {
@@ -540,12 +548,12 @@ foreach($Server in $Servers)
540
548
It " Should have the Log SP_WhoisActive to Table Agent Job $LogSPBlitzToTable " {
541
549
$Return.LogSPBlitzToTable | Should Be $LogSPBlitzToTable
542
550
}
543
- It " Log SP_Blitz to Table Agent Job Should Be $LogSPBlitzToTableEnabled " {
551
+ It " Log SP_Blitz to Table Agent Job Should Be Enabled " {
544
552
$Return.LogSPBlitzToTableEnabled | Should Be $LogSPBlitzToTableEnabled
545
553
}
546
- It " Log SP_Blitz to Table Agent Job Should Be $LogSPBlitzToTableScheduledScheduled " {
554
+ It " Log SP_Blitz to Table Agent Job Should Be Scheduled " {
547
555
$Return.LogSPBlitzToTableScheduled | Should Be $LogSPBlitzToTableScheduled
548
- }
556
+ }
549
557
It " Log SP_Blitz to Table Agent Job Should Be Scheduled $LogSPBlitzToTableSchedule " {
550
558
$Return.LogSPBlitzToTableSchedule.Value | Should Be $LogSPBlitzToTableSchedule
551
559
}
0 commit comments