-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsuresql-netcred.ps1
97 lines (80 loc) · 3.33 KB
/
suresql-netcred.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
param(
$server = "localhost",
$instance = "MSSQLSERVER",
$instancefull = "$server\$instance",
$minimumdb = 4
)
#$instancefull = "manual\override"
$connectionString = "Server=$instancefull;Integrated Security=True;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$failure = 1
$mustfail = $false
$q = 'use master;SELECT name, state FROM sys.databases;'
try {
$connection.Open()
write-host ("Connected on {0}" -f $connectionString)
#As long as there was no connection to the database, it is set 'online' until the point it has been conneted
#so first list the databases
#try to query there tables, and then reread the databases status
$datatable = @()
$cmd = $connection.CreateCommand()
$cmd.CommandText = $q
$reader = $cmd.ExecuteReader()
while($reader.Read()) {
$datatable += New-Object -TypeName psobject -Property @{name=$reader["name"];state=$reader["state"];}
}
$reader.Close()
if($datatable.Count -ge $minimumdb) {
$datatable | % {
$dbname = $_.name
$lcmd = $connection.CreateCommand()
$lcmd.CommandText = ("USE [{0}];select name from sys.tables;" -f $dbname )
try {
$null = $lcmd.ExecuteScalar()
} catch {
$mustfail = $true
write-host ("Something bad on {0}, ignoring for now {1}" -f $dbname,$error[0])
if($connection.state -ne 'Open')
{
write-host "Need to reopen the db, ..."
try { $connection.Open() } catch { write-host "Something is really bad, exiting with failure";exit 1 }
}
}
}
#rereading the table
$datatable = @()
$cmd = $connection.CreateCommand()
$cmd.CommandText = $q
$reader = $cmd.ExecuteReader()
while($reader.Read())
{
$datatable += New-Object -TypeName psobject -Property @{name=$reader["name"];state=$reader["state"];}
}
$reader.Close()
$connection.Close()
$allonline = $true
$off = @()
$datatable | % {
if ( $_.state -eq 0 ) {
write-host ("online : {0}" -f $_.name)
} else {
$allonline = $false
#state codes : https://msdn.microsoft.com/en-us/library/ms178534.aspx
write-host ("code {0} : {1}" -f $_.state,$_.name)
$off += $_.name
}
}
if ($allonline -and (-not $mustfail) ) { $failure = 0; write-host ("All {0} databases online !" -f $datatable.count) }
elseif ($allonline -and $mustfail) { $failure = 1;write-host ("All {0} databases online where online but initial query on all dbs failed somewhere, please look for ""Something bad on""" -f $datatable.count)}
else { $failure = 1;write-host ("Not online db detected : {0}" -f ($off -join ",")) }
} else {
$failure = 1;write-host ("Query should at least give back {0} databases" -f $minimumdb)
}
} catch {
write-host ("Query failed or could not open connection {1} : {0} " -f $error[0],$connectionString)
$failure = 1
} finally {
$connection.Close()
}
exit $failure