-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping-db.ps1
35 lines (25 loc) · 1.04 KB
/
ping-db.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
27
28
29
30
31
32
33
34
35
param(
[string] $serverName = $(throw 'serverName is required'),
[string] $database = 'master'
)
$connectionString = "Server=$serverName;Database=$database;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" }
}