I wrote a check for Nagios to detect if the currently installed Version of Java is the newest or if there are Updates to be applied.
First I get both the currently installed version of Java (using some code I found to retrieve the currently installed version) and the newest available version (using the document http://java.com/applet/JreCurrentVersion2.txt). Then I transform them using regular expressions (result: $1.$2.$3) to the same style, e.g.:
7.0.25
When printing the transformed versions via Wscript.Echo I can see, both are identical, but the String Comparison Operator StrComp() returns always false, if the Strings are equal or different. With an old version installed I get
Critical: Java Version 7.0.24 - available: 7.0.25
which is intended, but with the correct version I also get
Critical: Java Version 7.0.25 - available: 7.0.25
instead of
OK: Java Version 7.0.25
I attached the complete script down below:
check_java.vbs
On Error Resume Next
CONST rOK = 0
CONST rWarning = 1
CONST rCritical = 2
CONST rUnknown = 3
blnJavaInstalled = False
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProducts = objWMIService.ExecQuery("SELECT Version FROM Win32_Product")
For Each objProduct in colProducts
If Err.Number = 0 Then
If (InStr(UCase(objProduct.Name),"JAVA") And Not InStr(UCase(objProduct.Name),"UPDATER")) Then
blnJavaInstalled = True
version = objProduct.Version
End If
End If
Next
If blnJavaInstalled <> True Then
Wscript.Echo "No Java found."
Wscript.Quit(rUnknown)
End If
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "([0-9]+)\.([0-9]+).([0-9]*[1-9])0*"
curVersion = objRegEx.Replace(version, "$1.$2.$3")
Dim o
Set o = CreateObject("MSXML2.XMLHTTP")
o.open "GET", "http://java.com/applet/JreCurrentVersion2.txt", False
o.send
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "1\.([0-9]+)\.([0-9]+)_([0-9]+)"
newVersion = objRegEx.Replace(o.responseText, "$1.$2.$3")
If StrComp(curVersion, newVersion) Then
Wscript.Echo "OK: Java Version " & curVersion
Wscript.Quit(rOK)
Else
Wscript.Echo "Critical: Java Version " & curVersion & " - available: " & newVersion
Wscript.Quit(rCritical)
End If