| 1 | '######################################################################## |
|---|
| 2 | '# Name: Michel van Son (michelvs@phison.nl) # |
|---|
| 3 | '# Company: Phison Technologies # |
|---|
| 4 | '# Date: 21-02-2011 # |
|---|
| 5 | '# Purpose: External check script to check if updates are available # |
|---|
| 6 | '# Changes: [dd-mm-yyyy] Name | changes # |
|---|
| 7 | '######################################################################## |
|---|
| 8 | |
|---|
| 9 | '################################################################################################################ |
|---|
| 10 | '# Without an argument, this script will return the amount of available updates with exit code 0 # |
|---|
| 11 | '# When supplying arguments, the first argument is the warning treshold and the second is the critical treshold # |
|---|
| 12 | '# More or less then 2 arguments will result in an error with status Unknown(3) # |
|---|
| 13 | '################################################################################################################ |
|---|
| 14 | |
|---|
| 15 | Option Explicit |
|---|
| 16 | |
|---|
| 17 | 'Declare variables. |
|---|
| 18 | Dim valWarning |
|---|
| 19 | Dim valCritical |
|---|
| 20 | Dim objWMIService |
|---|
| 21 | Dim valUpdates |
|---|
| 22 | Dim updateSession |
|---|
| 23 | Dim updateSearcher |
|---|
| 24 | Dim searchResult |
|---|
| 25 | Dim I |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | 'Check for arguments. |
|---|
| 29 | if Wscript.Arguments.Count > 0 then |
|---|
| 30 | if Wscript.Arguments.Count > 2 then |
|---|
| 31 | Wscript.Echo "Too many arguments given. Please enter warning and critical tresholds." |
|---|
| 32 | Wscript.Quit(3) |
|---|
| 33 | elseif Wscript.Arguments.Count < 2 then |
|---|
| 34 | Wscript.Echo "Too few arguments given. Please enter warning and critical tresholds." |
|---|
| 35 | Wscript.Quit(3) |
|---|
| 36 | else |
|---|
| 37 | valWarning = Wscript.Arguments.Item(0) |
|---|
| 38 | valCritical = Wscript.Arguments.Item(1) |
|---|
| 39 | valWarning = CInt(valWarning) |
|---|
| 40 | valCritical = CInt(valCritical) |
|---|
| 41 | end if |
|---|
| 42 | end if |
|---|
| 43 | |
|---|
| 44 | 'Start searching for updates |
|---|
| 45 | Set updateSession = CreateObject("Microsoft.Update.Session") |
|---|
| 46 | Set updateSearcher = updateSession.CreateupdateSearcher() |
|---|
| 47 | Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'") |
|---|
| 48 | |
|---|
| 49 | valUpdates = searchResult.Updates.count - 1 |
|---|
| 50 | |
|---|
| 51 | 'Check the updates count, echo it and exit with proper exitcode. |
|---|
| 52 | if valUpdates > 0 then |
|---|
| 53 | if Not IsEmpty(valCritical) then |
|---|
| 54 | if valUpdates >= valCritical then |
|---|
| 55 | Wscript.Echo "Critical: applicable updates: " & valUpdates & "|'Updates'=" & valUpdates & ";" & valWarning & ";" & valCritical |
|---|
| 56 | Wscript.Quit(2) |
|---|
| 57 | elseif valUpdates >= valWarning then |
|---|
| 58 | Wscript.Echo "Warning: applicable updates: " & valUpdates & "|'Updates'=" & valUpdates & ";" & valWarning & ";" & valCritical |
|---|
| 59 | Wscript.Quit(1) |
|---|
| 60 | else |
|---|
| 61 | Wscript.Echo "OK: applicable updates: " & valUpdates & "|'Updates'=" & valUpdates & ";" & valWarning & ";" & valCritical |
|---|
| 62 | Wscript.Quit(0) |
|---|
| 63 | end if |
|---|
| 64 | else |
|---|
| 65 | Wscript.Echo "OK: applicable updates: " & valUpdates & "|'Updates'=" & valUpdates |
|---|
| 66 | Wscript.Quit(0) |
|---|
| 67 | end if |
|---|
| 68 | else |
|---|
| 69 | Wscript.Echo "OK: No applicable updates.|'updates'=" & valUpdates |
|---|
| 70 | Wscript.Quit(0) |
|---|
| 71 | end if |
|---|