String comparison in VBScript

10,303

Solution 1

o.responseText has a trailing newline, so you're actually comparing 7.0.25 to 7.0.25\r\n, which obviously are not equal. You can reveal the trailing newline with something like this:

>>> WScript.Echo "_" & o.responseText & "_"
_7.0.25
_

Either change your second regular expression to

objRegEx.Pattern = "1\.([0-9]+)\.([0-9]+)_([0-9]+)[\s\S]*"

or remove the newline from o.responseText before doing the regexp replacement:

newVersion = objRegEx.Replace(Replace(o.responseText, vbNewLine, ""), "$1.$2.$3")

Solution 2

StrComp returns 0 (False) if the strings compared are equal. So switch the branches of your If statement. As you are not interested in which of the strings is greater or smaller, consider using the = operator (less risk of mistake).

Share:
10,303
Salandas
Author by

Salandas

Updated on June 04, 2022

Comments

  • Salandas
    Salandas almost 2 years

    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
    
  • Salandas
    Salandas almost 11 years
    Thanks, this was the part I was looking for.
  • Salandas
    Salandas almost 11 years
    I experimented with this as well, but since the Strings were always different (see marked answers), I couldn't resolve the issue with inserting " = 0" to StrComp, so I reverted the changes I did. With the solution in the other answer and your information, I got it working. Thanks