Giter VIP home page Giter VIP logo

Comments (3)

alagoutte avatar alagoutte commented on June 19, 2024 1

Nice

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

from powerfgt.

Cool34000 avatar Cool34000 commented on June 19, 2024 1

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

from powerfgt.

Cool34000 avatar Cool34000 commented on June 19, 2024 1

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"
        }
    }
}

:-)

from powerfgt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.