FortiPower/PowerFGT

Upgrade Path

Cool34000 opened this issue · 3 comments

Hi,

Here's a litte function I wrote to compute upgrade path.

Function Get-FirmwareUpdate{
    $firmware = (Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware)
    $FortiOS  = $firmware.results.current   | select version
    $Update   = $firmware.results.available | select version -First 1
    if($Update){
        if($FortiOS -eq $Update){# Firmware is up to date
            [pscustomobject]@{
                "Installed" = $($FortiOS.version)
                "Available" = $($Update.version)
            }
        }else{# Firmware is not up to date, compute the upgrade path
            $major       = $firmware.results.current.major
            $minor       = $firmware.results.current.minor
            $patch       = $firmware.results.current.patch
            $updateMajor = ($firmware.results.available | select -First 1).major
            $updateMinor = ($firmware.results.available | select -First 1).minor
            $updatePatch = ($firmware.results.available | select -First 1).patch
            $upgradePath = "v$($major).$($minor).$($patch)"
            Do{
                $nextFirmware = Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware/upgrade-paths | select -ExpandProperty results | where { $_.from.major -eq $major -and $_.from.minor -eq $minor -and $_.from.patch -eq $patch } | select -First 1
                $major        = $nextFirmware.to.major
                $minor        = $nextFirmware.to.minor
                $patch        = $nextFirmware.to.patch
                $upgradePath = $upgradePath + " -> v$($major).$($minor).$($patch)"
            }Until($major -eq $updateMajor -and $minor -eq $updateMinor -and $patch -eq $updatePatch)
            [pscustomobject]@{
                "Installed"    = $($FortiOS.version)
                "Available"    = $($Update.version)
                "Upgrade Path" = $upgradePath
            }
        }
    }else{# No firmware available (support expired)
        [pscustomobject]@{
            "Installed" = $($FortiOS.version)
            "Available" = "N/A"
        }
    }
}
Get-FirmwareUpdate

Installed Available Upgrade Path
--------- --------- ------------
v6.0.13   v7.0.5    v6.0.13 -> v6.2.10 -> v6.4.8 -> v7.0.5

Nice

I get look for add a cmdlet for add monitor/system/firmware

Hi,

Just find out that when FortiOS is up to date, it returns the previous version instead of the current.
This is a fix for this behaviour.
Also add the upgrade path if you want to stay in the same firmware branch.

Function Get-FirmwareUpdate{
    $firmware            = (Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware)
    $FortiOS             = $firmware.results.current   | select version
    $CurrentMajor        = $firmware.results.current.major
    $CurrentMinor        = $firmware.results.current.minor
    $CurrentPatch        = $firmware.results.current.patch
    $CurrentVersion      = "$($CurrentMajor)$($CurrentMinor)$($CurrentPatch)"
    $CurrentVersion      = $CurrentVersion -as [int]
    $FullUpdate          = $firmware.results.available | select version -First 1
    $FullUpdateMajor     = ($firmware.results.available | select -First 1).major
    $FullUpdateMinor     = ($firmware.results.available | select -First 1).minor
    $FullUpdatePatch     = ($firmware.results.available | select -First 1).patch
    $FullUpdateVersion   = "$($FullUpdateMajor)$($FullUpdateMinor)$($FullUpdatePatch)"
    $FullUpdateVersion   = $FullUpdateVersion -as [int]
    $BranchUpdate        = $firmware.results.available | Where-Object { $_.major -eq $CurrentMajor -and $_.minor -eq $CurrentMinor } | select version -First 1
    $BranchUpdateMajor   = ($firmware.results.available | Where-Object { $_.major -eq $CurrentMajor -and $_.minor -eq $CurrentMinor } | select -First 1).major
    $BranchUpdateMinor   = ($firmware.results.available | Where-Object { $_.major -eq $CurrentMajor -and $_.minor -eq $CurrentMinor } | select -First 1).minor
    $BranchUpdatePatch   = ($firmware.results.available | Where-Object { $_.major -eq $CurrentMajor -and $_.minor -eq $CurrentMinor } | select -First 1).patch
    $BranchUpdateVersion = "$($BranchUpdateMajor)$($BranchUpdateMinor)$($BranchUpdatePatch)"
    $BranchUpdateVersion = $BranchUpdateVersion -as [int]
    if($FullUpdate){
        if($CurrentVersion -ge $FullUpdateVersion){# FortiOS is fully up to date
            [pscustomobject]@{
                "Installé"   = $($FortiOS.version)
                "Disponible" = "No update available"
            }
        }else{
            if($CurrentVersion -lt $BranchUpdateVersion){# FortiOS update available in the same firmware branch
                $upgradePath = "v$($CurrentMajor).$($CurrentMinor).$($CurrentPatch)"
                Do{
                    $nextFirmware = Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware/upgrade-paths | select -ExpandProperty results | where { $_.from.major -eq $CurrentMajor -and $_.from.minor -eq $CurrentMinor -and $_.from.patch -eq $CurrentPatch -and $_.to.major -eq $BranchUpdateMajor -and $_.to.minor -eq $BranchUpdateMinor } | select -First 1
                    $major        = $nextFirmware.to.major
                    $minor        = $nextFirmware.to.minor
                    $patch        = $nextFirmware.to.patch
                    $upgradePath  = $upgradePath + " -> v$($major).$($minor).$($patch)"
                }Until($major -eq $BranchUpdateMajor -and $minor -eq $BranchUpdateMinor -and $patch -eq $BranchUpdatePatch)
                [pscustomobject]@{
                    "Installed"    = $($FortiOS.version)
                    "Available"    = $($BranchUpdate.version)
                    "Upgrade Path" = $upgradePath
                }
            }
            if(($CurrentVersion -lt $FullUpdateVersion) -and ($BranchUpdateVersion -ne $FullUpdateVersion)){# FortiOS update available in a superior firmware branch
                $upgradePath = "v$($CurrentMajor).$($CurrentMinor).$($CurrentPatch)"
                Do{
                    $nextFirmware = Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware/upgrade-paths | select -ExpandProperty results | where { $_.from.major -eq $CurrentMajor -and $_.from.minor -eq $CurrentMinor -and $_.from.patch -eq $CurrentPatch } | select -First 1
                    $major        = $nextFirmware.to.major
                    $minor        = $nextFirmware.to.minor
                    $patch        = $nextFirmware.to.patch
                    $upgradePath  = $upgradePath + " -> v$($major).$($minor).$($patch)"
                }Until($major -eq $FullUpdateMajor -and $minor -eq $FullUpdateMinor -and $patch -eq $FullUpdatePatch)
                [pscustomobject]@{
                    "Installed"    = $($FortiOS.version)
                    "Available"    = $($FullUpdate.version)
                    "Upgrade Path" = $upgradePath
                }
            }
        }
    }else{# No firmware available (support expired)
        [pscustomobject]@{
            "Installed" = $($FortiOS.version)
            "Available" = "No firmware available, check your Fortiguard support"
        }
    }
}
Get-FirmwareUpdate
Installed Available Upgrade Path
-------- ---------- ------------
v6.4.7   v6.4.8     v6.4.7 -> v6.4.8
v6.4.7   v7.0.5     v6.4.7 -> v7.0.5
Get-FirmwareUpdate
Installed Available Upgrade Path
-------- ---------- ------------
v6.2.9   v6.2.10     v6.2.9 -> v6.2.10

Thanks to your private message, here's a shortest and optimized version!

Function Get-FirmwareUpdate{
    $firmware       = (Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware)
    $FortiOS        = $firmware.results.current   | select version
    $CurrentVersion = [version]"$($firmware.results.current.major).$($firmware.results.current.minor).$($firmware.results.current.patch)"
    $FullUpdate     = $firmware.results.available | select version -First 1
    if($FullUpdate){
        $FullUpdateVersion   = [version]"$(($firmware.results.available | select -First 1).major).$(($firmware.results.available | select -First 1).minor).$(($firmware.results.available | select -First 1).patch)"
        $BranchUpdate        = $firmware.results.available | Where-Object { $_.major -eq $CurrentVersion.Major -and $_.minor -eq $CurrentVersion.Minor } | select version -First 1
        $BranchUpdateVersion = [version]"$(($firmware.results.available | Where-Object { $_.major -eq $CurrentVersion.Major -and $_.minor -eq $CurrentVersion.Minor } | select -First 1).major).$(($firmware.results.available | Where-Object { $_.major -eq $CurrentVersion.Major -and $_.minor -eq $CurrentVersion.Minor } | select -First 1).minor).$(($firmware.results.available | Where-Object { $_.major -eq $CurrentVersion.Major -and $_.minor -eq $CurrentVersion.Minor } | select -First 1).patch)"
        if($CurrentVersion -ge $FullUpdateVersion){
            [pscustomobject]@{
                "Installed" = $($FortiOS.version)
                "Available" = "No update available"
            }
        }else{
            if($CurrentVersion -lt $BranchUpdateVersion){
                $upgradePath = "v$($CurrentVersion.Major).$($CurrentVersion.Minor).$($CurrentVersion.Build)"
                $major       = $CurrentVersion.Major
                $minor       = $CurrentVersion.Minor
                $patch       = $CurrentVersion.Build
                Do{
                    $nextFirmware = Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware/upgrade-paths | select -ExpandProperty results | where { $_.from.major -eq $major -and $_.from.minor -eq $minor -and $_.from.patch -eq $patch -and $_.to.major -eq $BranchUpdateVersion.Major -and $_.to.minor -eq $BranchUpdateVersion.Minor } | select -First 1
                    $major        = $nextFirmware.to.major
                    $minor        = $nextFirmware.to.minor
                    $patch        = $nextFirmware.to.patch
                    $upgradePath  = $upgradePath + " -> v$($major).$($minor).$($patch)"
                }Until($major -eq $BranchUpdateVersion.Major -and $minor -eq $BranchUpdateVersion.Minor -and $patch -eq $BranchUpdateVersion.Build)
                [pscustomobject]@{
                    "Installed"    = $($FortiOS.version)
                    "Available"    = $($BranchUpdate.version)
                    "Upgrade Path" = $upgradePath
                }
            }
            if(($CurrentVersion -lt $FullUpdateVersion) -and ($BranchUpdateVersion -ne $FullUpdateVersion)){
                $upgradePath = "v$($CurrentVersion.Major).$($CurrentVersion.Minor).$($CurrentVersion.Build)"
                $major       = $CurrentVersion.Major
                $minor       = $CurrentVersion.Minor
                $patch       = $CurrentVersion.Build
                Do{
                    $nextFirmware = Invoke-FGTRestMethod -uri api/v2/monitor/system/firmware/upgrade-paths | select -ExpandProperty results | where { $_.from.major -eq $major -and $_.from.minor -eq $minor -and $_.from.patch -eq $patch } | select -First 1
                    $major        = $nextFirmware.to.major
                    $minor        = $nextFirmware.to.minor
                    $patch        = $nextFirmware.to.patch
                    $upgradePath  = $upgradePath + " -> v$($major).$($minor).$($patch)"
                }Until($major -eq $FullUpdateVersion.Major -and $minor -eq $FullUpdateVersion.Minor -and $patch -eq $FullUpdateVersion.Build)
                [pscustomobject]@{
                    "Installed"    = $($FortiOS.version)
                    "Available"    = $($FullUpdate.version)
                    "Upgrade Path" = $upgradePath
                }
            }
        }
    }else{
        [pscustomobject]@{
            "Installed" = $($FortiOS.version)
            "Available" = "No firmware available, check your Fortiguard support"
        }
    }
}

:-)