Version Comparison Function and OS Readiness

Eltord
Contributor

Hi everyone.

My company is working on getting ready to allow the Catalina update to flow through the masses, but we have some very hard blocking requirements, specifically some software minimum versions that have to be met before allowing the machines to upgrade. Our goal was to scope out all the devices that have the required applications updated to these minimum version numbers, but of course currently Smart Groups are not able to do version math as a criteria.

To accommodate this, I wrote a function that can take the applications information (name, minimum target version, current installed version) and the amount of segments in the version number, then does the mathematical comparisons to each segment and determines if the installed version is higher or lower than the minimum targeted version. Returns False if not met, returns True if the minimum version is met. Here is that function:

compareVersions() {

    #initiate named variables from passed.
    ACTIVE_VER="$1"
    TARGET_VER="$2"
    TARGET_POSITION_COUNT="$3"

    #initiating "i" variable
    i=0

    #initiating the target and active arrays for the loop
    targetArray=($(echo $TARGET_VER | tr "." "
"))
    activeArray=($(echo $ACTIVE_VER | tr "." "
"))

    log "###Beginning comparison loop###"

    # loop through each segment (array item) and compare on each separately.
    while [ $i -le $(($TARGET_POSITION_COUNT-1)) ]
    do
        log "Target array value is: ${targetArray[$i]}"
        log "Active array value is: ${activeArray[$i]}"

        #casting array value into integer
        targetNum=$((${targetArray[$i]}+0))
        activeNum=$((${activeArray[$i]}+0))

        # if active array value is not equal to the targeted array value, check if its greater than or less than and
        # then return a true/false value accordingly
        if [ $activeNum != $targetNum ]
        then
            log "$activeNum is not equal to $targetNum, checking if less or greater than"
            if [ $activeNum -gt $targetNum ]
            then
                log "$activeNum is greater than $targetNum, returning TRUE"
                return_value="TRUE"
            elif [ $activeNum -lt $targetNum ]
            then
                log "$activeNum is less than $targetNum, returning FALSE"
                return_value="FALSE"
            fi
        else
            # numbers are equal, so minimum value of this segment is being met, moving onto next segment.
            log "$activeNum is equal to $targetNum, moving onto next number"
        fi

        # Check to see if there is a returned value now from the previous if statement. If not, adds 1 to i and checks if
        # i equals the positions count now. If they do, logic has determined that all segmented numbers are equal, meaning
        # minimum has been met, so ending the loop with a true value to return. Otherwise, ending the loop with the already
        # passed return value.
        if [ -z $return_value ]
        then
            ((i++))
            if [ $i -eq $TARGET_POSITION_COUNT ]
            then
                log "$i is the same as $TARGET_POSITION_COUNT, ran out of numbers to check so they are all equal, returning TRUE"
                return_value="TRUE"
            fi
        else
            log "a return value was passed, matching $i to $TARGET_POSITION_COUNT to break the loop"
            i=$TARGET_POSITION_COUNT
        fi
    done

    log "###Ending Loop###"

    #ending function by returning the return value from previous loop.
    log "Return value is: $return_value"
    echo $return_value
}

For my purposes, I've implemented this into a script that checks multiple applications and their versions, then returns a final True or False if all the minimums are met or not. This now sits in an Extension Attribute for Catalina Readiness, and then that EA is now the criteria for the smart group that i can then use to scope to any software update reset policies and such that I need.

I've uploaded this script i'm using to my github here if you'd like to use this function in a similar manner. There are examples of two applications in the script to make it easier to understand how to customize it to your needs.

0 REPLIES 0