In dbachecks we enable people to see what checks are available by running Get-DbcCheck. This gives a number of properties including the ‘type’ of check. This refers to the configuration item or parameter that is required to have a value for this check to run.
For example – Any check to do with SQL Agent is of type Sqlinstance because it requires an instance to be specified but a check for SPN is of type ComputerName because it requires a computer name to run.
Automation for the win
Because I believe in automation I do not want to have to hard code these values anywhere but create them when the module is imported so we use a json file to feed Get-DbcCheck and populate the Json file when we import the module. This is done using the method that I described here and means that whenever a new check is added it is automatically available in Get-DbcCheck without any extra work.
## Parse the file with AST $CheckFileAST = [Management.Automation.Language.Parser]::ParseInput($check, [ref]$null, [ref]$null) ## Old code we can use the describes $Describes = $CheckFileAST.FindAll([Func[Management.Automation.Language.Ast, bool]] { param ($ast) $ast.CommandElements -and $ast.CommandElements[0].Value -eq 'describe' }, $true) @($describes).ForEach{ $groups += $filename $Describe = $_.CommandElements.Where{$PSItem.StaticType.name -eq 'string'}[1] $title = $Describe.Value $Tags = $PSItem.CommandElements.Where{$PSItem.StaticType.name -eq 'Object[]' -and $psitem.Value -eq $null}.Extent.Text.ToString().Replace(', $filename', '') # CHoose the type if ($Describe.Parent -match "Get-Instance") { $type = "Sqlinstance" } elseif ($Describe.Parent -match "Get-ComputerName" -or $Describe.Parent -match "AllServerInfo") { $type = "ComputerName" } elseif ($Describe.Parent -match "Get-ClusterObject") { $Type = "ClusteNode" }
Describe "Backup Path Access" -Tags BackupPathAccess, Storage, DISA, $filename { @(Get-Instance).ForEach{ if ($NotContactable -contains $psitem) { Context "Testing Backup Path Access on $psitem" { It "Can't Connect to $Psitem" { $false| Should -BeTrue -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-DbaSqlPath-SqlInstance $psitem -Path $backuppath| Should -BeTrue -Because 'The SQL Service account needs to have access to the backup path to backup your databases' } } } } }
Until Rob breaks it!
SPNs
Disk Space
Ping Computer
CPUPrioritisation
Disk Allocation Unit
Instance Connection
Describe "Server Power Plan Configuration" -Tags PowerPlan, $filename { @(Get-ComputerName).ForEach{ } } Describe "Instance Connection" -Tags InstanceConnection, Connectivity, $filename { @(Get-Instance).ForEach{ } } Describe "SPNs" -Tags SPN, $filename { @(Get-ComputerName).ForEach{ } } Describe "Disk Space" -Tags DiskCapacity, Storage, DISA, $filename { @(Get-ComputerName).ForEach{ } } Describe "Ping Computer" -Tags PingComputer, $filename { @(Get-ComputerName).ForEach{ } } Describe "CPUPrioritisation" -Tags CPUPrioritisation, $filename { @(Get-ComputerName).ForEach{ } } Describe "Disk Allocation Unit" -Tags DiskAllocationUnit, $filename { @(Get-ComputerName).ForEach{ } }
@(Get-ComputerName).ForEach{ Describe "Server Power Plan Configuration" -Tags PowerPlan, $filename { } Describe "SPNs" -Tags SPN, $filename { } Describe "Disk Space" -Tags DiskCapacity, Storage, DISA, $filename { } Describe "Ping Computer" -Tags PingComputer, $filename { } Describe "CPUPrioritisation" -Tags CPUPrioritisation, $filename { } Describe "Disk Allocation Unit" -Tags DiskAllocationUnit, $filename { } } Describe "Instance Connection" -Tags InstanceConnection, Connectivity, $filename { @(Get-Instance).ForEach{ } }
elseif ($Describe.Parent-match"Get-ComputerName"-or$Describe.Parent-match"AllServerInfo")
AST for other things
$ComputerNameForEach = $CheckFileAST.FindAll([Func[Management.Automation.Language.Ast, bool]] { param ($ast) $ast -is [System.Management.Automation.Language.InvokeMemberExpressionAst] }, $true)
## New code uses a Computer Name loop to speed up execution so need to find that as well $ComputerNameForEach=$CheckFileAST.FindAll([Func[Management.Automation.Language.Ast,bool]] { param ($ast) $ast-is [System.Management.Automation.Language.InvokeMemberExpressionAst] -and$ast.expression.Subexpression.Extent.Text-eq'Get-ComputerName' }, $true).Extent and now I can match the Tags to the type again :-) if ($ComputerNameForEach-match$title) { $type="ComputerName" }
Install-Module dbachecks -Scope CurrentUser