CheckExternalScripts: check_updates_available.vbs

File check_updates_available.vbs, 2.8 KB (added by mike2k, 2 years ago)
Line 
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
15Option Explicit
16
17'Declare variables.
18Dim valWarning
19Dim valCritical
20Dim objWMIService
21Dim valUpdates
22Dim updateSession
23Dim updateSearcher
24Dim searchResult
25Dim I
26
27
28'Check for arguments.
29if 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
42end if
43
44'Start searching for updates
45Set updateSession = CreateObject("Microsoft.Update.Session")
46Set updateSearcher = updateSession.CreateupdateSearcher()
47Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
48
49valUpdates = searchResult.Updates.count - 1
50
51'Check the updates count, echo it and exit with proper exitcode.
52if 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
68else
69        Wscript.Echo "OK: No applicable updates.|'updates'=" & valUpdates
70        Wscript.Quit(0)
71end if