Created
February 11, 2025 16:51
-
-
Save steviecoaster/838eef7f6a9f5f70654fdfaab07aed75 to your computer and use it in GitHub Desktop.
Test SQL connection strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-SqlConnectionString { | |
<# | |
.SYNOPSIS | |
Tests establishing a connection to SQL instance usese provided ConnectionString | |
.DESCRIPTION | |
Long description | |
.PARAMETER ConnectionString | |
The ConnectionString attempting to make a connection | |
.EXAMPLE | |
Test-SqlConnectionString -ConnectionString 'Server=Localhost\SQLEXPRESS;Database=master;Trusted_Connction=true;' | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory)] | |
[String] | |
$ConnectionString | |
) | |
end { | |
$connection = [System.Data.SqlClient.SqlConnection]::new() | |
$connection.ConnectionString = $ConnectionString | |
try { | |
$null = $connection.Open() | |
return $true | |
} | |
catch { | |
return $false | |
} | |
finally { | |
$connection.Dispose() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment