-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_odbc_query.ps1
More file actions
64 lines (50 loc) · 1.59 KB
/
check_odbc_query.ps1
File metadata and controls
64 lines (50 loc) · 1.59 KB
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
# Source from
# https://blog.netways.de/2011/11/07/odbc-datenbankzugriff-mit-powershell/
Param(
[string]$server,
[string]$database,
[string]$uid,
[string]$pwd,
[string]$query,
[string]$warning, # warning if x is greater than
[string]$critical # critical if x is greater than
)
$DBDSN="Driver={SQL Server};Server=$server;Database=$database;UID=$uid;PWD=$pwd;"
#Datenbankverbindungsobjekt instanzieren
$DBConnection=New-Object System.Data.Odbc.OdbcConnection
#DSN im Objekt setzen
$DBConnection.ConnectionString=$DBDSN
#Datenbankverbindung öffnen
$DBConnection.Open()
#Datenbankbefehlsobjekt instanzieren
$DBCommand=New-Object System.Data.Odbc.OdbcCommand
#Datenbankverbindung in Befehlsobjekt setzen
$DBCommand.Connection=$DBConnection
#Query angeben, dabei muss _QUERY_ durch das gewünschte Query ersetzt werden
$DBCommand.CommandText="$query"
#Ein Reader-Objekt erzeugen
$DBResult=$DBCommand.ExecuteReader()
#Anzahl der Rows zählen
$DBCounter=$DBResult.FieldCount
#Rows durchiterieren
while ($DBResult.Read()) {
for ($i = 0; $i -lt $DBCounter; $i++) {
if ($DBResult.GetValue($i) -lt $warning) {
$result = $DBResult.GetValue($i)
Write-Host "OK -" $result"|query="$result";$warning;$critical"
exit 0
}
ElseIf ($DBResult.GetValue($i) -lt $critical) {
$result = $DBResult.GetValue($i)
Write-Host "Warning -" $result"|query="$result";$warning;$critical"
exit 1
}
Else {
$result = $DBResult.GetValue($i)
Write-Host "Critical -" $result"|query="$result";$warning;$critical"
exit 2
}
}
}
#Datenbankverbindung schließen
$DBConnection.Close()