Create a Powershell script to automatically lookup and download HP printer firmware files

Bastiaan Oosterman 21 Reputation points
2022-05-05T16:51:00.59+00:00

Hi,

I want to create a Powershell script that I can run periodically to automatically download the latest HP printer firmware files.

The source where I can find the path to the latest firmware file for each printer model is this URL:
http://ftp.ext.hp.com//pub/networking/software/pfirmware/pfirmware.glf

One item for example:

[HP LaserJet 600 M601/M602/M603]
SupportedModels = HP LaserJet M601n; HP LaserJet M601dn;HP LaserJet M602n; HP LaserJet M602dn; HP LaserJet M602x; HP LaserJet 600 M602;HP LaserJet M603n; HP LaserJet M603dn; HP LaserJet M603xh; HP LaserJet 600 M602; HP LaserJet 600 M603; HP LaserJet 600 M601;
Path = http://ftp.hp.com/pub/networking/software/pfirmware/ljM601_602_603_fs3.9.9_2309059_000599.bdl
Name = ljM601_602_603_fs3.9.9_2309059_000599.bdl
Size = 86072636
Version = 2309059_000599 (FutureSmart Bundle Version 3.9.9)
Revision = 2309059_000599
Date = 20220214
Asset Identifier = 736664-0015

I would like the script to download the firmware of some printer models that I can specify beforehand, so I don't need all the firmware files.

Unfortunately I don't have much knowledge of how to create Powershell script, so I hope someone has an example for me similar to this that I can reuse.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,671 Reputation points
    2022-05-06T19:23:33.03+00:00

    This should work:

    $desiredmodels = "HP Color LaserJet 2605","HP LaserJet 3050 AIO","SL-K4350LX"
    $OutputDirectory = "c:\junk\"
    [array]$Downloads = @()
    
    $x=Invoke-WebRequest http://ftp.ext.hp.com//pub/networking/software/pfirmware/pfirmware.glf
    
    $inaprinter = $false
    $info= @{}
    
    ForEach ($l in ($x.content -split ("`r`n"))) {
        ForEach-Object{
            if ($l.Trim().Length -lt 1){
                Continue                    # skip empty lines
            }
            if ($l.substring(0,2) -eq ";;"){
                # do nothing, it's just a comment
                Continue
            }
    
            if ($l.substring(0,1) -eq "["){ # the start of a new printer data set
                if ($inaprinter -and $info['download'] -eq $true){  # if there's a completed record, save it
                    $Downloads += [PSCustomObject]$info
                }
                $inaprinter = $true
                $info.clear()
                $info['download'] = $false
                Continue                        # not interested in the name, just the fact that it's the beginning
            }
            $l = $l -replace " \t+="," ="            # some data sets place tabs immediately before the '='! Remove them.
            [array]$p = $l -split " = "
            if ($p.count -ne 2){        # only interested in lines like X = Y
                Continue
            }
            switch ($p[0]) {
                "SupportedModels" {
                    [array]$models = $p[1].split(";",[System.StringSplitOptions]::RemoveEmptyEntries)
                    if ($models.count -gt 0){
                        ForEach ($model in $models){
                            if ($desiredmodels -contains $model){
                                $info['download'] = $true
                            }
                        }
                    }
                    break
                }
                "Path" {$info[$p[0]] = $p[1]; break}
                "Name" {$info[$p[0]] = $p[1]; break}
            }
        }
    }
    if ($inaprinter -and $info['download'] -eq $true){  # in case there's pending record
        $Downloads += [PSCustomObject]$info
    }
    $Downloads |
        ForEach-Object{
            Invoke-WebRequest -Uri $_.Path -OutFile "$OutputDirectory$($_.Name)"
        }
    

0 additional answers

Sort by: Most helpful