Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/brianary/scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
brianary committed Nov 17, 2023
2 parents 00594d9 + 67f52be commit 2a69774
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 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,23 +48,38 @@ 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],
serverproperty('InstanceName') [InstanceName],
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}
}

0 comments on commit 2a69774

Please sign in to comment.