From 4c32ce9a65c58ccbcca87536c834a1fe9f4bd2b6 Mon Sep 17 00:00:00 2001 From: Brian Lalonde Date: Tue, 14 Nov 2023 20:36:24 -0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=F0=9F=94=92=EF=B8=8F=20Secure=20p?= =?UTF-8?q?assword=20value;=20return=20false=20if=20connection=20fails?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test-ConnectionString.ps1 | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Test-ConnectionString.ps1 b/Test-ConnectionString.ps1 index 5cf5ede0..5b871281 100644 --- a/Test-ConnectionString.ps1 +++ b/Test-ConnectionString.ps1 @@ -52,11 +52,13 @@ LocalSMOVersion : 17.100.0.0 ) Process { - $csb = New-DbaConnectionStringBuilder -ConnectionString $ConnectionString - $server = Connect-DbaInstance -ConnectionString $ConnectionString - $conn = Join-Keys.ps1 -ReferenceObject (New-Object Collections.Hashtable $csb) ` - -InputObject (Test-DbaConnection $csb.DataSource -SkipPSRemoting |ConvertTo-OrderedDictionary.ps1) - $info = Invoke-DbaQuery -SqlInstance $server -As PSObject -Query @' + try + { + $csb = New-DbaConnectionStringBuilder -ConnectionString $ConnectionString + $server = Connect-DbaInstance -ConnectionString $ConnectionString + $conn = Join-Keys.ps1 -ReferenceObject (New-Object Collections.Hashtable $csb) ` + -InputObject (Test-DbaConnection $csb.DataSource -SkipPSRemoting |ConvertTo-OrderedDictionary.ps1) + $info = Invoke-DbaQuery -SqlInstance $server -As PSObject -Query @' select @@ServerName [ServerName], db_name() [DatabaseName], serverproperty('ComputerNamePhysicalNetBIOS') [ComputerName], serverproperty('MachineName') [MachineName], @@ -64,7 +66,10 @@ select @@ServerName [ServerName], db_name() [DatabaseName], current_timestamp [ServerTime], serverproperty('Edition') [Edition], app_name() [AppName]; -'@ |Add-Member -NotePropertyName Server -NotePropertyValue $server -PassThru | - ConvertTo-OrderedDictionary.ps1 - return [pscustomobject](Join-Keys.ps1 $conn $info) +'@ |ConvertTo-OrderedDictionary.ps1 + if($info.ContainsKey('Password')) {$info['Password'] = ConvertTo-SecureString $info['Password'] -AsPlainText -Force} + [void] $info.Add('Server', $server) + return [pscustomobject](Join-Keys.ps1 $conn $info) + } + catch {return $false} } From f771c352d37d4eff3de30d8ad0301fd117189f6e Mon Sep 17 00:00:00 2001 From: Brian Lalonde Date: Tue, 14 Nov 2023 21:27:54 -0800 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20Simplify=20default=20return=20v?= =?UTF-8?q?alue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test-ConnectionString.ps1 | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Test-ConnectionString.ps1 b/Test-ConnectionString.ps1 index 5b871281..1819fa41 100644 --- a/Test-ConnectionString.ps1 +++ b/Test-ConnectionString.ps1 @@ -9,7 +9,7 @@ System.Management.Automation.PSObject containing properties about the connection Database .EXAMPLE -Test-ConnectionString.ps1 'Server=(localdb)\ProjectsV13;Integrated Security=SSPI;Encrypt=True' +Test-ConnectionString.ps1 'Server=(localdb)\ProjectsV13;Integrated Security=SSPI;Encrypt=True' -Details ServerName : SERVERNAME\LOCALDB#DCCC9EEC AppName : Core Microsoft SqlClient Data Provider @@ -48,17 +48,20 @@ LocalSMOVersion : 17.100.0.0 #Requires -Version 3 #Requires -Modules dbatools [CmdletBinding()][OutputType([psobject])] Param( -[Parameter(Position=0,Mandatory=$true)][string] $ConnectionString +[Parameter(Position=0,Mandatory=$true)][string] $ConnectionString, +[switch] $Details ) Process { try { - $csb = New-DbaConnectionStringBuilder -ConnectionString $ConnectionString - $server = Connect-DbaInstance -ConnectionString $ConnectionString - $conn = Join-Keys.ps1 -ReferenceObject (New-Object Collections.Hashtable $csb) ` - -InputObject (Test-DbaConnection $csb.DataSource -SkipPSRemoting |ConvertTo-OrderedDictionary.ps1) - $info = Invoke-DbaQuery -SqlInstance $server -As PSObject -Query @' + if($Details) + { + $csb = New-DbaConnectionStringBuilder -ConnectionString $ConnectionString + $server = Connect-DbaInstance -ConnectionString $ConnectionString + $conn = Join-Keys.ps1 -ReferenceObject (New-Object Collections.Hashtable $csb) ` + -InputObject (Test-DbaConnection $csb.DataSource -SkipPSRemoting |ConvertTo-OrderedDictionary.ps1) + $info = Invoke-DbaQuery -SqlInstance $server -As PSObject -Query @' select @@ServerName [ServerName], db_name() [DatabaseName], serverproperty('ComputerNamePhysicalNetBIOS') [ComputerName], serverproperty('MachineName') [MachineName], @@ -67,9 +70,15 @@ select @@ServerName [ServerName], db_name() [DatabaseName], serverproperty('Edition') [Edition], app_name() [AppName]; '@ |ConvertTo-OrderedDictionary.ps1 - if($info.ContainsKey('Password')) {$info['Password'] = ConvertTo-SecureString $info['Password'] -AsPlainText -Force} - [void] $info.Add('Server', $server) - return [pscustomobject](Join-Keys.ps1 $conn $info) + if($info.ContainsKey('Password')) {$info['Password'] = ConvertTo-SecureString $info['Password'] -AsPlainText -Force} + [void] $info.Add('Server', $server) + return [pscustomobject](Join-Keys.ps1 $conn $info) + } + else + { + return Invoke-DbaQuery -SqlInstance (Connect-DbaInstance -ConnectionString $ConnectionString) ` + -Query 'select cast(1 as bit) Success;' |ConvertFrom-DataRow.ps1 -AsValues + } } catch {return $false} } From 67f52bedfa39cdae6384fea5d258520df0688132 Mon Sep 17 00:00:00 2001 From: Brian Lalonde Date: Wed, 15 Nov 2023 09:55:57 -0800 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20Fix=20connection=20string=20?= =?UTF-8?q?test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test-ConnectionString.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Test-ConnectionString.ps1 b/Test-ConnectionString.ps1 index 1819fa41..8186809d 100644 --- a/Test-ConnectionString.ps1 +++ b/Test-ConnectionString.ps1 @@ -70,9 +70,10 @@ select @@ServerName [ServerName], db_name() [DatabaseName], serverproperty('Edition') [Edition], app_name() [AppName]; '@ |ConvertTo-OrderedDictionary.ps1 - if($info.ContainsKey('Password')) {$info['Password'] = ConvertTo-SecureString $info['Password'] -AsPlainText -Force} [void] $info.Add('Server', $server) - return [pscustomobject](Join-Keys.ps1 $conn $info) + $connInfo = Join-Keys.ps1 $conn $info + if($connInfo.Contains('Password')) {$connInfo['Password'] = ConvertTo-SecureString $connInfo['Password'] -AsPlainText -Force} + return [pscustomobject]$connInfo } else {