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.


[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

$serverConn = new-object '' $connectionString

trap {
write-host -foreground red "Failed to contact $serverName"
write-host ('[{0}] {1}' -f

$sqlCommand = new-object '' $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" }


Skip to main content