diff --git a/Test-ConnectionString.ps1 b/Test-ConnectionString.ps1 index 5cf5ede..8186809 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,15 +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 { - $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 + { + 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], @@ -64,7 +69,17 @@ 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 + [void] $info.Add('Server', $server) + $connInfo = Join-Keys.ps1 $conn $info + if($connInfo.Contains('Password')) {$connInfo['Password'] = ConvertTo-SecureString $connInfo['Password'] -AsPlainText -Force} + return [pscustomobject]$connInfo + } + else + { + return Invoke-DbaQuery -SqlInstance (Connect-DbaInstance -ConnectionString $ConnectionString) ` + -Query 'select cast(1 as bit) Success;' |ConvertFrom-DataRow.ps1 -AsValues + } + } + catch {return $false} }