Skip to content

Commit c615956

Browse files
Enabled Instances for Test-SQLDefault
1 parent a79f182 commit c615956

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

Test-SQLDefaults.ps1

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
This will run Pester tests against SQLServer1 instance and check using all the variables
2424
.EXAMPLE
2525
$Parms = @{
26-
Servers = 'SQLServer1','SQLServer2','SQLServer3';
26+
Servers = 'SQLServer1','SQLServer2','SQLServer2\Instance1','SQLServer3';
2727
SQLAdmins = 'THEBEARD\Rob','THEBEARD\SQLAdmins';
2828
BackupDirectory = 'C:\MSSQL\Backup';
2929
DataDirectory = 'C:\MSSQL\Data\';
@@ -57,20 +57,20 @@
5757
5858
Test-SQLDefault @Parms
5959
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
6161
.NOTES
6262
AUTHOR : Rob Sewell http://sqldbawithabeard.com
6363
Initial 12/05/2016
6464
#>
6565
function Test-SQLDefault {
6666
[CmdletBinding()]
6767
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
6969
[Parameter(Mandatory = $true,
7070
ValueFromPipeline = $true,
7171
ValueFromPipelineByPropertyName = $true,
7272
Position = 0)]
73-
[string]$Servers ,
73+
$Servers ,
7474
# Expected SQL Admin Account or an array of accounts
7575
[Parameter(Mandatory = $true,
7676
ValueFromPipeline = $true,
@@ -264,13 +264,29 @@ param(
264264
[string]$LogSPBlitzToTableStartTime
265265
)
266266
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+
268279
## 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"
271282
$_
272283
continue
273284
}
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}
274290

275291
Describe "$Server" {
276292
BeforeAll {
@@ -299,7 +315,7 @@ foreach($Server in $Servers)
299315
$Return.RestoreProc = $Sps -contains 'RestoreCommand'
300316
$Return.OlaSysFullEnabled = $srv.JobServer.jobs['DatabaseBackup - SYSTEM_DATABASES - FULL'].IsEnabled
301317
$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
303319
$Return.OlaSysFullStartTime = $srv.JobServer.jobs['DatabaseBackup - SYSTEM_DATABASES - FULL'].JobSchedules.ActiveStartTimeOfDay
304320
$Return.OlaUserFullEnabled = $srv.JobServer.jobs['DatabaseBackup - USER_DATABASES - FULL'].IsEnabled
305321
$Return.OlaUserFullScheduled = $srv.JobServer.jobs['DatabaseBackup - USER_DATABASES - FULL'].HasSchedule
@@ -337,23 +353,17 @@ foreach($Server in $Servers)
337353
$Return.SysDatabasesFullBackupToday = $srv.Databases.Where{$_.IsSystemObject -eq $true -and $_.Name -ne 'tempdb' -and $_.LastBackupDate -lt (Get-Date).AddDays(-1)}.Count
338354
Return $Return
339355
}
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
348357
}
349358
Context 'Server' {
350359
It 'Should Exist and respond to ping' {
351-
$connect = Test-Connection $Server -count 1 -Quiet
360+
$connect = Test-Connection $ServerName -count 1 -Quiet
352361
$Connect|Should Be $true
353362
}
363+
if($connect -eq $false){break}
354364
It 'Should have SQL Server Installed' {
355365
$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
357367
$State | Should Be $true
358368
}
359369
} # End Context
@@ -362,54 +372,55 @@ foreach($Server in $Servers)
362372
(Get-Service -ComputerName $Server -Name MSSQLServer).Status | Should Be 'Running'
363373
}
364374
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'
366376
}
367377
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'
369379
}
370380
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'
372382
}
373383
} # End Context
374-
Context 'FireWall' {
384+
Context 'FireWall' {
385+
375386
It 'Should have a Firewall connection for SQL Browser' {
376387
$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
379390
}
380391
It 'Firewall connection for SQL Browser should be enabled' {
381392
$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
383394
$State | Should Be $true
384395
}
385396
It 'SQL Browser Firewall Action Should Be Allow' {
386397
$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
388399
$State.value | Should Be 'Allow'
389400
}
390401
It 'SQL Browser Firewall Application should be the SQLBrowser.exe' {
391402
$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
393404
$State | Should Be 'C:\Program Files (x86)\Microsoft SQL Server\90\Shared\sqlbrowser.exe'
394405
}
395406
It 'Should have a Firewall connection for SQL DB Engine' {
396407
$Scriptblock = {Get-NetFirewallRule -Name 'SQL Database Engine - Allow'}
397-
$State = Invoke-Command -ComputerName $Server -ScriptBlock $Scriptblock
408+
$State = Invoke-Command -ComputerName $ServerName -ScriptBlock $Scriptblock
398409
$State | Should Be $true
399410
}
400411
It 'Firewall connection for SQL DB Engine should be enabled' {
401412
$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
403414
$State | Should Be $true
404415
}
405416
It 'DB EngineFirewall Action Should Be Allow' {
406417
$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
408419
$State.value | Should Be 'Allow'
409420
}
410421
It 'DB EngineFirewall Application should be the SQLBrowaser.exe' {
411422
$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
413424
$State | Should Be 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\sqlservr.exe'
414425
}
415426
} # End Context Firewall
@@ -420,7 +431,7 @@ foreach($Server in $Servers)
420431
It 'Databases should have a normal Status - No Restoring, Recovery Pending etc' {
421432
$Return.DatabasesStatus |Should Be 0
422433
}
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' {
424435
$Return.SysDatabasesFullBackupToday | SHould be 0
425436
}
426437
} # End Context
@@ -460,9 +471,6 @@ foreach($Server in $Servers)
460471
It 'Should have alerts for 823,824 and 825' {
461472
$Return.Alerts82345Exist |Should Be 3
462473
}
463-
It 'Alerts for 823,824 and 825 should be enebled' {
464-
$Return.Alerts82345Enabled |Should Be 3
465-
}
466474
} # End Context
467475
Context 'Agent Jobs' {
468476
It 'Should have Agent Jobs' {
@@ -540,12 +548,12 @@ foreach($Server in $Servers)
540548
It "Should have the Log SP_WhoisActive to Table Agent Job $LogSPBlitzToTable" {
541549
$Return.LogSPBlitzToTable| Should Be $LogSPBlitzToTable
542550
}
543-
It "Log SP_Blitz to Table Agent Job Should Be $LogSPBlitzToTableEnabled" {
551+
It "Log SP_Blitz to Table Agent Job Should Be Enabled" {
544552
$Return.LogSPBlitzToTableEnabled| Should Be $LogSPBlitzToTableEnabled
545553
}
546-
It "Log SP_Blitz to Table Agent Job Should Be $LogSPBlitzToTableScheduledScheduled" {
554+
It "Log SP_Blitz to Table Agent Job Should Be Scheduled" {
547555
$Return.LogSPBlitzToTableScheduled| Should Be $LogSPBlitzToTableScheduled
548-
}
556+
}
549557
It "Log SP_Blitz to Table Agent Job Should Be Scheduled $LogSPBlitzToTableSchedule" {
550558
$Return.LogSPBlitzToTableSchedule.Value| Should Be $LogSPBlitzToTableSchedule
551559
}

0 commit comments

Comments
 (0)