Skip to content

Commit

Permalink
✨ Simplify default return value
Browse files Browse the repository at this point in the history
  • Loading branch information
brianary committed Nov 15, 2023
1 parent 4c32ce9 commit f771c35
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions Test-ConnectionString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand All @@ -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}
}

0 comments on commit f771c35

Please sign in to comment.