Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created February 11, 2025 16:51
Show Gist options
  • Save steviecoaster/838eef7f6a9f5f70654fdfaab07aed75 to your computer and use it in GitHub Desktop.
Save steviecoaster/838eef7f6a9f5f70654fdfaab07aed75 to your computer and use it in GitHub Desktop.
Test SQL connection strings
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