Permalink
Cannot retrieve contributors at this time
FUNCTION Test-SqlConnection { | |
<# | |
.SYNOPSIS | |
Test connection to SQL Instance | |
.DESCRIPTION | |
Test connection to SQL Instance | |
.PARAMETER Instance | |
The name of the instance you wish to check connections to | |
.EXAMPLE | |
PS C:\> Test-SQLConnection -Instance DEV-MSSQL | |
.NOTES | |
Author : Ryan DeVries | |
Last Updated: 2015/06/01 | |
Version : 1 | |
.INPUTS | |
[string] | |
.OUTPUTS | |
[boolean] | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Position=0,Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName,HelpMessage="The name of the instance")] | |
[ValidateNotNullorEmpty()] | |
[string]$Instance | |
) | |
begin { | |
Import-SQLPS | |
Write-Verbose "Detected parameter set $($PSCmdlet.ParameterSetName)" | |
$scriptstring = "Starting $($MyInvocation.MyCommand)" | |
foreach ($param in $PSBoundParameters.GetEnumerator()){ $scriptstring += " -$($param.key) $($param.value)"} | |
Write-Verbose $scriptstring | |
} | |
process { | |
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" $instance | |
$srv.ConnectionContext.ConnectTimeout = 5 | |
try { | |
Write-Verbose "Trying to connect to $instance" | |
# Try and connect to server | |
$srv.ConnectionContext.Connect() | |
Write-Verbose "Connection successful! Disconnecting from $instance" | |
$srv.ConnectionContext.Disconnect() | |
return $true | |
} | |
catch { | |
Write-Verbose "Could not connect to $instance" | |
return $false | |
} | |
} | |
end { Write-Verbose "Ending $($MyInvocation.Mycommand)" } | |
} |