ping-db.ps1 - quick ping to see if you can talk to a database instance
Very simple, just contact a SQL Server instance to see if it can do the CPU-heavy calculation of 2+2 :) I put this together just to verify that I could use server=hostname,port when connecting to a SQL Server instance on a particular port.
param(
[string] $serverName = $(throw 'serverName is required')
)
$connectionString = "Server=$serverName;Database=master;Integrated Security=SSPI"
write-host "Attempting to contact via connection string $connectionString"
# Formulate the query we'll run
$query = 'select 2+2'
# Load the System.Data assembly for access to SQL Server
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Data")
$serverConn = new-object 'system.data.sqlclient.SqlConnection' $connectionString
trap {
write-host -foreground red "Failed to contact $serverName"
write-host ('[{0}] {1}' -f
$_.exception.getbaseexception().gettype().name,
$_.exception.getbaseexception().message)
continue
}
&{
$serverConn.Open()
$sqlCommand = new-object 'system.data.sqlclient.SqlCommand' $query, $serverConn
$result = $sqlCommand.ExecuteScalar()
if ($result -eq 4) { write-host -foreground green "Successful ping of server $serverName" }
else { throw "unexpected result from query: $result" }
}