forked from JMNetwalker/AzureSQLConnectivityChecker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSQLConnectivityTest.ps1
78 lines (69 loc) · 3.64 KB
/
SQLConnectivityTest.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
$DatabaseServer = "xxxxx.database.windows.net"
$Database = "xxxxxx"
$Username = "xxxxxx"
$Password = "xxxxxx"
$Pooling = $true
$NumberExecutions =10
$File = "E:\TSQL.SQL"
cls
$sw = [diagnostics.stopwatch]::StartNew()
for ($i=0; $i -lt $NumberExecutions; $i++)
{
try
{
$connectionString = "Server=tcp:$DatabaseServer,1433;Initial Catalog=$Database;Persist Security Info=False;User ID=$Username;Password=$Password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30"
if( $Pooling -eq $true )
{
$connectionString = $connectionString + ";Pooling=True"
}
else
{
$connectionString = $connectionString + ";Pooling=False"
}
$ExistFile= Test-Path $File
if($ExistFile -eq 1)
{
$query = @(Get-Content $File)
}
else
{
$query = @("SELECT 1")
}
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$connection.StatisticsEnabled = 1
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand
$command.CommandTimeout = 60
$command.Connection=$connection
for ($iQuery=0; $iQuery -lt $query.Count; $iQuery++)
{
$start = get-date
$connection.Open()
$command.CommandText = $query[$iQuery]
$command.ExecuteNonQuery() | Out-Null
$connection.Close()
$end = get-date
$data = $connection.RetrieveStatistics()
write-Output "-------------------------" | Out-File e:\my.log -Append -width 300
write-Output ("Query : " + $query[$iQuery]) | Out-File e:\my.log -Append -width 300
write-Output ("Iteration : " +$i) | Out-File e:\my.log -Append -Width 300
write-Output ("Time required (ms) : " +(New-TimeSpan -Start $start -End $end).TotalMilliseconds) | Out-File e:\my.log -Append -Width 300
write-Output ("NetworkServerTime (ms): " +$data.NetworkServerTime) | Out-File e:\my.log -Append -Width 300
write-Output ("Execution Time (ms) : " +$data.ExecutionTime) | Out-File e:\my.log -Append -Width 300
write-Output ("Connection Time : " +$data.ConnectionTime) | Out-File e:\my.log -Append -Width 300
write-Output ("ServerRoundTrips : " +$data.ServerRoundtrips) | Out-File e:\my.log -Append -Width 300
write-Output ("BuffersReceived : " +$data.BuffersReceived) | Out-File e:\my.log -Append -Width 300
write-Output ("SelectRows : " +$data.SelectRows) | Out-File e:\my.log -Append -Width 300
write-Output ("SelectCount : " +$data.SelectCount) | Out-File e:\my.log -Append -Width 300
write-Output ("BytesSent : " +$data.BytesSent) | Out-File e:\my.log -Append -Width 300
write-Output ("BytesReceived : " +$data.BytesReceived) | Out-File e:\my.log -Append -Width 300
write-Output "-------------------------" | Out-File e:\my.log -Append -Width 300
}
}
catch
{
Write-Output -ForegroundColor DarkYellow "You're WRONG" | Out-File e:\my.log -Append -Width 300
Write-Output -ForegroundColor Magenta $Error[0].Exception | Out-File e:\my.log -Append -Width 300
}
}
write-Output ("Time spent (ms) Procces : " +$sw.elapsed) | Out-File e:\my.log -Append -Width 300
write-Output ("Review: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql/provider-statistics-for-sql-server") | Out-File e:\my.log -Append -Width 300