创建用于 Windows 系统的定制评估程序

如果基本评估程序未使用正确的评估条件对先决条件属性的期望值和实际值进行比较,那么您可以创建 VBScript 评估程序。 创建定制评估程序时,文件名必须以 _compare 结尾,并且文件必须存储在 /Windows 子目录中。 有需要时,定制评估程序可以使用通用函数和子例程对值进行比较。

开始之前

在创建评估程序之前,请务必查看下列附录中的函数及子例程集合。 请确定能否使用其中的任何函数及子例程来比较值:
注: 通用函数passOrFail()可以对下列数据类型的实际值和期望值进行比较:常规数字、以 MB 或 GB 为单位的大小、以 MHz 或 GHz 为单位的处理器速度、布尔值或字符串。 仅当无法使用 passOrFail 函数时,才应创建定制评估程序。

过程

  1. 创建 VBScript 文件。 通过使用以下文件命名约定的变体,将此文件保存在 ips_root/Windows 目录中:
    [prefix_identifier.]property_name[.suffix_identifier]_compare.vbs
    其中:
    • prefix_identifier表 1 中概述的先决条件属性预定义类别的相应标识。 此前缀标识是某些预定义类别所必需的。
    • property_name 是先决条件属性的名称。
    • suffix_identifier表 1 中概述的先决条件属性子类型的相应可选标识。
  2. 添加代码,对使用 VBScript COM 和相关函数作为自变量传递到评估程序的实际值和期望值进行比较。 请确保此比较返回如下所示的标准输出:
    • "PASS"(如果先决条件属性的期望值等于或大于其实际值)
    • "FAIL"(如果先决条件属性的期望值与其实际值不等)
    注: 此工具在运行评估程序后分析该先决条件属性的严重性级别。 如果该先决条件属性的严重性级别设置为 WARN,或者实际值在某个受支持先决条件属性的期望值范围之内,那么此工具将相关的 FAIL 结果重新调整为 WARN请参阅严重性级别
  3. 运行定制评估程序以确保不存在运行时错误,必要时进行调试。

示例

此定制评估程序将检查 Tivoli® Directory Integrator 版本的实际值和期望值。 它使用了通用函数versionCompare()

wscript.echo "expect: " & wscript.arguments(0)
wscript.echo "real value: " & wscript.arguments(1)
wscript.echo tdiVersionCompare(wscript.arguments(0), wscript.arguments(1))

function tdiVersionCompare(expect, real)
    if len(real) = 0 then
        tdiVersionCompare = "FAIL"
        exit function
    end if
    
    expect = Trim(expect)
    real = Trim(real)
    
    Dim expectedVersion
    'if (StrComp(Right(expect,1),"+")=0 or StrComp(Right(expect,1),"-")=0) Then
    if (Right(expect,1)="+" or Right(expect,1)="-") Then
        expectedVersion = Left(expect,len(expect)-1)
    else 
        expectedVersion = expect
    end if
        
    Dim cmp
    cmp = versionCompare(expectedVersion,real)
    
    if (StrComp(Right(expect,1),"+")=0) Then
        ' Version must be at least expected value
        if (cmp=0 or cmp=-1) Then
            tdiVersionCompare = "PASS"
        else 
            tdiVersionCompare = "FAIL"
        end if
    elseif (StrComp(Right(expect,1),"-")=0) Then
            ' Version must be less than or equal to expected value
            if (cmp=0 or cmp=1) Then
                tdiVersionCompare = "PASS"
            else 
                tdiVersionCompare = "FAIL"
            end if
    elseif cmp=0 then
        tdiVersionCompare = "PASS"
    else
        tdiVersionCompare = "FAIL"
    end if
end function

' Generic function for comparing 2 version strings
'
' Parameters
'       ver1 The first version string
'       ver2 The second version string
'
' ver1 and ver2 are expected to be dot-separated version strings 
'(e.g. 1.0.0.4, 2.3, 3.40.26.7800, 2.3.a)Version strings can have any 
' number of parts. When comparing versions with different numbers of 
' parts, missing parts of the shorter version string will be treated 
' as if there was a zero there. If any non-numeric characters are 
' included in a version part, those corresponding parts will be compared 
' asstrings and not parsed into numeric form
'
' Returns
'       1 version1 > version2
'      -1 version1 < version2
'       0 version1 = version2
'
' Special cases:
' RESULT    version 1    version 2
'   0         empty        empty
'   1      validString     empty
'  -1         empty     validString
'
' NOTE: This function should eventually move to common_functions.vbs
  
function versionCompare(ver1, ver2)
    WScript.echo "Comparing [" & ver1 & "] to [" & ver2 & "]"
    
    Const UNASSIGNED = "*UNASSIGNED*"
    Dim v1Default, v2Default
    
    ' Handle special cases:
    if (IsEmpty(ver1) and IsEmpty(ver2)) Then
        versionCompare = 0
        exit function
    end if
    if (IsEmpty(ver1) and not IsEmpty(ver2)) Then
        versionCompare = -1
        exit function
    end if
    if (not IsEmpty(ver1) and IsEmpty(ver2)) Then
        versionCompare = 1
        exit function
    end if    
    
    Dim ver1Parts, ver2Parts
    
    ' Versions are not empty.  Break into parts and compare numbers
    ver1Parts = Split(ver1,".")
    ver2Parts = Split(ver2,".")
    
    Dim v1Size, v2Size
    v1Size = ubound(ver1Parts)
    v2Size = ubound(ver2Parts)
    
    ' If last version part is "*", treat all missing parts as "*" 
    '(so 2.* matches 2.1.3, for example)
    if (v1Size > v2Size) Then
        Redim Preserve ver2Parts(v1Size)
        if (ver2Parts(v2Size)="*") Then
            for i = v2Size to v1Size
                ver2Parts(i) = "*"
            next
        end if
    elseif (v2Size > v1Size) Then
        Redim Preserve ver1Parts(v2Size)
        if (ver1Parts(v1Size)="*") Then
            for i = v1Size to v2Size
                ver1Parts(i) = "*"
            next
        end if
    end if
    
    Dim i
    i = 0
    
    Do While (i<=ubound(ver1Parts) or i<=ubound(ver2Parts))
        Dim v1, v2, v1Str, v2Str
                
        v1Str = UNASSIGNED
        v2Str = UNASSIGNED
               
        if (i<=ubound(ver1Parts)) Then
            on error resume next
            v1 = Int(ver1Parts(i))
            if not Err=0 Then
                v1Str = ver1Parts(i)
                if (i<=ubound(ver2Parts)) Then
                    v2Str = ver2Parts(i)
                else 
                    v2Str = "0"
                end if
            end if
        else 
            v1 = 0
        end if
        
        if (i<=ubound(ver2Parts)) Then
            on error resume next
            v2 = Int(ver2Parts(i))
            if not Err=0 Then
                if (i<=ubound(ver1Parts)) Then
                    v1Str = ver1Parts(i)
                else 
                    v1Str = "0"
                end if
                v2Str = ver2Parts(i)
            end if
        else 
            v2 = 0
        end if
        
        if (not v1Str=UNASSIGNED or not v2Str=UNASSIGNED) Then                            
            if (IsEmpty(v1Str)) Then
                v1Str = "0"
            end if
            if (IsEmpty(v2Str)) Then
                v2Str = "0"
            End if
            
            'WScript.echo "Comparing as strings: " & v1Str & " : " & v2Str            
            ' Compare as Strings if either part could not be converted to a number
            if (not v1Str="*" and not v2Str="*") Then
                if (not v1Str=v2Str) Then
                    versionCompare = StrComp(v1Str,v2Str)
                    exit function
                end if
            end if
        else
            'WScript.echo "Comparing as numbers: " & v1 & " : " & v2

            if (v1 > v2) Then
                versionCompare = 1
                exit function
            end if
            if (v2 > v1) Then
                versionCompare = -1
                exit function
            end if
        end if
    
        i = i + 1
    Loop
    
    ' If we got here, versions must be equal
    versionCompare = 0
    
end function