-
Notifications
You must be signed in to change notification settings - Fork 4
/
Test-SQLConnection.ps1
26 lines (25 loc) · 1 KB
/
Test-SQLConnection.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Function Test-SQLConnection
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)] $DatabaseInstance,
[Parameter(ParameterSetName="SQL", Mandatory=$true)] [System.Management.Automation.PSCredential] $Credential,
[Parameter(ParameterSetName="Windows")] [System.Management.Automation.SwitchParameter] $UserWindowsAuth
);
if($UserWindowsAuth.IsPresent) {
$connectionString = "Data Source={0};Integrated Security=true;Initial Catalog=master;Connect Timeout=3;" -f $DatabaseInstance
}
else {
$connectionString = "Data Source={0};Integrated Security=false;Initial Catalog=master;Connect Timeout=3;User={1};Password={2}" -f $DatabaseInstance, $Credential.UserName, $Credential.GetNetworkCredential().Password
}
$sqlConn = New-Object System.Data.SqlClient.SqlConnection $connectionString
$sqlConn.Open()
if ($sqlConn.State -eq 'Open')
{
$sqlConn.Close();
Write-Output $true
}
else {
Write-Output $false
}
}