Skip to content

Commit

Permalink
so we can check instance connections #882
Browse files Browse the repository at this point in the history
  • Loading branch information
SQLDBAWithABeard committed Feb 11, 2023
1 parent 0ffeb2e commit 62df56d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
10 changes: 6 additions & 4 deletions source/checks/Instancev5.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,34 @@ BeforeDiscovery {
$__dbcconfig = Get-DbcConfig
}

Describe "Instance Connection" -Tags InstanceConnection, Connectivity, High, Instance -ForEach $InstancesToTest {
Describe "Instance Connection" -Tag InstanceConnection, Connectivity, High, Instance -ForEach $InstancesToTest {
BeforeAll {
$skipall = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection' }).Value
$skipremote = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.remoting' }).Value
$skipping = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.ping' }).Value
$skipauth = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.auth' }).Value
$authscheme = ($__dbcconfig | Where-Object { $_.Name -eq 'policy.connection.authscheme' }).Value
}
BeforeDiscovery {
$authscheme = ($__dbcconfig | Where-Object { $_.Name -eq 'policy.connection.authscheme' }).Value
}

Context "Checking Instance Connection on on <_.Name>" -Skip:$skipall {
It "connects successfully to <_.Name>" -Skip:skipall {
It "connects successfully to <_.Name>" -Skip:$skipall {
$PsItem.InstanceConnection.Connect | Should -BeTrue -Because "We expect the instance to be connectable"
}
It "auth scheme should be $authscheme on <_.Name>" -Skip:$skipauth {
$PsItem.InstanceConnection.AuthScheme | Should -Be $authscheme -Because "We expect the auth scheme to be $authscheme"
}
It "We should be able to ping host <_.Name>" -Skip:$skipping {
$PsItem.InstanceConnection.Ping | Should -BeTrue -Because "We expect the instance to be pingable"
$PsItem.InstanceConnection.Ping | Should -Be 'Success' -Because "We expect the instance to be pingable"
}
It "We should be able to remote onto <_.Name>" -Skip:$skipremote {
$PsItem.InstanceConnection.Remote | Should -BeTrue -Because "We expect the instance to be remotely connectable"
}
}
}


Describe "Default Trace" -Tag DefaultTrace, CIS, Low, Instance -ForEach $InstancesToTest {
$skip = ($__dbcconfig | Where-Object { $_.Name -eq 'skip.instance.defaulttrace' }).Value
Context "Checking Default Trace on <_.Name>" {
Expand Down
11 changes: 8 additions & 3 deletions source/internal/configurations/configuration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ Register-PSFConfigValidation -Name validation.EmailValidation -ScriptBlock $Emai

# some configs to help with autocompletes and other module level stuff
#apps
$defaultRepo = (Convert-Path -Path "$script:ModuleRoot\checks")
$defaultRepo = "$script:ModuleRoot\checks"
Set-PSFConfig -Module dbachecks -Name app.checkrepos -Value @($defaultRepo) -Initialize -Description "Where Pester tests/checks are stored"
Set-PSFConfig -Module dbachecks -Name app.sqlinstance -Value $null -Initialize -Description "List of SQL Server instances that SQL-based tests will run against"
Set-PSFConfig -Module dbachecks -Name app.computername -Value $null -Initialize -Description "List of Windows Servers that Windows-based tests will run against"
Set-PSFConfig -Module dbachecks -Name app.sqlcredential -Value $null -Initialize -Description "The universal SQL credential if Trusted/Windows Authentication is not used"
Set-PSFConfig -Module dbachecks -Name app.wincredential -Value $null -Initialize -Description "The universal Windows if default Windows Authentication is not used"

Set-PSFConfig -Module dbachecks -Name app.localapp -Value (Convert-Path -Path "$env:localappdata\dbachecks") -Initialize -Description "Persisted files live here"
Set-PSFConfig -Module dbachecks -Name app.maildirectory -Value (Convert-Path -Path "$env:localappdata\dbachecks\dbachecks.mail") -Initialize -Description "Files for mail are stored here"
if ($IsLinux) {
Set-PSFConfig -Module dbachecks -Name app.localapp -Value "$home\dbachecks" -Initialize -Description "Persisted files live here"
Set-PSFConfig -Module dbachecks -Name app.maildirectory -Value "$home\dbachecks\dbachecks.mail" -Initialize -Description "Files for mail are stored here"
} else {
Set-PSFConfig -Module dbachecks -Name app.localapp -Value "$env:localappdata\dbachecks" -Initialize -Description "Persisted files live here"
Set-PSFConfig -Module dbachecks -Name app.maildirectory -Value "$env:localappdata\dbachecks\dbachecks.mail" -Initialize -Description "Files for mail are stored here"
}

Set-PSFConfig -Module dbachecks -Name app.cluster -Value $null -Initialize -Description "One host name for each cluster for the HADR checks"

Expand Down
70 changes: 36 additions & 34 deletions source/internal/functions/NewGet-AllInstanceInfo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -274,41 +274,43 @@ function NewGet-AllInstanceInfo {
$TempDBTest = Test-DbaTempDbConfig -SqlInstance $Instance
}
'InstanceConnection' {
#local is always NTLM except when its a container ;-)
if ($Instance.NetBiosName -eq $ENV:COMPUTERNAME -and ($instance.Name -notlike '*,*')) {
if (-not(($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.auth' }).Value)) {
$authscheme = $instance.Query("Select auth_scheme as AuthScheme FROM sys.dm_exec_connections WHERE session_id = @@SPID").AuthScheme
} else {
$authscheme = 'skipped'
}
} else {
$authscheme = 'skipped-local'
}

if (-not(($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.ping' }).Value)) {
$pingu = New-Object System.Net.NetworkInformation.Ping
$timeout = 1000 #milliseconds
$ping = ($pingu.Send($instance.ComputerName, $timeout)).Status
} else {
$ping = 'skipped'
}


if (-not(($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.remote' }).Value)) {
#simple remoting check
try {
$null = Invoke-Command -ComputerName $instance.ComputerName -ScriptBlock { Get-ChildItem } -ErrorAction Stop
$remote = $true
} catch {
$remote = $false
}
} else {
$remote = 'skipped'
}

$InstanceConnection = @{
connection = $true # because we wouldnt get here otherwise
authscheme = (
#local is always NTLM except when its a container ;-)
if ($Instance.NetBiosName -eq $ENV:COMPUTERNAME -and ($instance.Name -notlike '*,*')) {
if (-not(($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.auth' }).Value)) {
$instance.Query("Select auth_scheme as AuthScheme FROM sys.dm_exec_connections WHERE session_id = @@SPID").AuthScheme
} else {
'skipped'
}
} else {
'skipped-local' }
)
ping = (
if (-not(($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.ping' }).Value)) {
$ping = New-Object System.Net.NetworkInformation.Ping
$timeout = 1000 #milliseconds
($ping.Send($instance.ComputerName, $timeout)).Status
} else {
'skipped'
}
)
remote = (
if (-not(($__dbcconfig | Where-Object { $_.Name -eq 'skip.connection.remote' }).Value)) {
#simple remoting check
try {
$null = Invoke-Command -ComputerName $instance.ComputerName -ScriptBlock { Get-ChildItem } -ErrorAction Stop
$true
} catch {
$false
}
} else {
'skipped'
}
)
Connect = $true # because we wouldnt get here otherwise
AuthScheme = $authscheme
Ping = $ping
Remote = $remote
}
}

Expand Down

0 comments on commit 62df56d

Please sign in to comment.