From 4d47bd67b23534e13274777435a3c4ff562a36b6 Mon Sep 17 00:00:00 2001 From: Andreas Jordan <66946165+andreasjordan@users.noreply.github.com> Date: Thu, 11 Apr 2024 07:25:43 +0200 Subject: [PATCH] Connect-DbaInstance - Connect DAC to test connection as soon as possible (#9305) --- public/Connect-DbaInstance.ps1 | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/public/Connect-DbaInstance.ps1 b/public/Connect-DbaInstance.ps1 index 886a9ff359..02ceb42836 100644 --- a/public/Connect-DbaInstance.ps1 +++ b/public/Connect-DbaInstance.ps1 @@ -948,14 +948,12 @@ function Connect-DbaInstance { # But ConnectionContext.IsOpen does not tell the truth if the instance was just shut down # And we don't use $server.ConnectionContext.Connect() as this would create a non pooled connection # Instead we run a real T-SQL command and just SELECT something to be sure we have a valid connection and let the SMO handle the connection - if (-not $DedicatedAdminConnection) { - try { - Write-Message -Level Debug -Message "We connect to the instance by running SELECT 'dbatools is opening a new connection'" - $null = $server.ConnectionContext.ExecuteWithResults("SELECT 'dbatools is opening a new connection'") - Write-Message -Level Debug -Message "We have a connected server object" - } catch { - Stop-Function -Target $instance -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Continue - } + try { + Write-Message -Level Debug -Message "We connect to the instance by running SELECT 'dbatools is opening a new connection'" + $null = $server.ConnectionContext.ExecuteWithResults("SELECT 'dbatools is opening a new connection'") + Write-Message -Level Debug -Message "We have a connected server object" + } catch { + Stop-Function -Target $instance -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Continue } if ($AzureUnsupported -and $server.DatabaseEngineType -eq "SqlAzureDatabase") {