-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathazure-sql-query.ps1
53 lines (47 loc) · 1.43 KB
/
azure-sql-query.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<#
script to test sql connectivity to azure sql paas.
with default query of 'select 1', output will be '1'
#>
param(
$username = "cloudadmin",
$password = "n0tMiP@ssw0rD",
$database = "testAzureSqlDb",
$port = 1433,
$query = "select 1",
[switch]$checkPorts
)
$VerbosePreference = $DebugPreference = "continue"
$ErrorActionPreference = "continue"
$error.Clear()
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$databaseFqdn = "$database.database.windows.net"
$sqlConnection.ConnectionString = "Server=tcp:$databaseFqdn,$port;
Initial Catalog=$database;
Persist Security Info=False;
User ID=$username;
Password=$password;
MultipleActiveResultSets=False;
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=30;"
$sqlConnection.Open()
$sqlCmd = new-object System.Data.SqlClient.SqlCommand($query, $sqlConnection)
$sqlReader = $sqlCmd.ExecuteReader()
$Counter = $sqlReader.FieldCount
while ($sqlReader.Read())
{
for ($i = 0; $i -lt $Counter; $i++)
{
write-host $sqlReader.GetName($i), $sqlReader.GetValue($i)
}
}
$sqlConnection.Close()
if($checkPorts)
{
Test-NetConnection -ComputerName $databaseFqdn -Port $port
write-host "checking sql redirect"
Test-NetConnection -ComputerName $databaseFqdn -Port 11000 # first
Test-NetConnection -ComputerName $databaseFqdn -Port 11999 # last
}
$VerbosePreference = $DebugPreference = "silentlycontinue"
write-host "finished"