Automating Lenovo Driver Downloads (Part 1)

by Dec 29, 2021

Many hardware vendors offer web-based self-service portals. Here is an example taken from Lenovo that returns detailed information about driver and other update downloads:

https://download.lenovo.com/cdrt/tools/drivermatrix/dm.html

If you need to manage hundreds or thousands of machines or need to regularly lookup information, you would of course want to automate this resource. The typical first approach would be to examine the HTML source code and search for a web service, or if all else fails, use Invoke-RestMethod and session cookies to send form data and mimick the user input.

This is not just complex, it can even fail altogether. The Lenovo website for example uses Javascript to compose the web frontend so PowerShell and Invoke-RestMethod are of no help here. You would have to use a Selenium-based test browser or other advanced web browser automation.

When you do look at the HTML source code carefully, though, you may come across lines like this:

$.get(“../../../catalog/” + x + “_” + document.getElementById(“os”).value + “.xml”, function(data, status)

Apparently, the data displayed on the website comes from static XML files so there is really no need to automate a web interface. All you would need to know in this case is the naming scheme of these XML files.

Here is the PowerShell function wrapped around these XML documents. It returns Lenovo driver information for any model and is fully automatable:

function Get-LenovoDriver
{
     param
     (
          $Model = '20JN',
          
          $Os = 'Win7',
          
          $Category = '*'
     )
     
     
     
     
     $url = "https://download.lenovo.com/catalog/${model}_$os.xml"
     $info = $model, $os
     $url = "https://download.lenovo.com/catalog/{0}_{1}.xml" -f $info
     
     $xml = Invoke-RestMethod -Uri $url -UseBasicParsing
     $data = [xml]$xml.Substring(3)
     [xml]$data = $xml.Substring(3)



     $data.packages.package | 
          Where-Object { $_.Category -like $Category } |
          ForEach-Object {
          $location = $_.location
          $name = $_.category
          $rohdaten = Invoke-RestMethod -Uri $location -UseBasicParsing
          [xml]$info = $rohdaten.Substring(3)
          $filename = $info.Package.Files.Installer.File.Name
          [PSCustomObject]@{
               Category = $name
               Command = $info.package.ExtractCommand
               Space = $info.package.DiskspaceNeeded
               Reboot = $info.package.Reboot.type
               Version = $info.package.version
               Download = "https://download.lenovo.com/pccbbs/mobiles/$filename"
               Datum = Get-Date
          }
     }
}

Instead of manually fiddling with a HTML page, you now can retrieve the information right from the PowerShell command line, for example:

 
PS> Get-LenovoDriver -Model 20JN -Os Win10 | Out-GridView -PassThru


Category : ThinkVantage Technology
Command  : n1msk20w.exe /VERYSILENT /DIR=%PACKAGEPATH% /EXTRACT="YES"
Space    : 33206066
Reboot   : 3
Version  : 1.82.0.20
Download : https://download.lenovo.com/pccbbs/mobiles/n1msk20w.exe
Datum    : 09.12.2021 13:16:00  

Even if you don’t have to manage Lenovo hardware, you may be able to reuse some of the techniques illustrated in this example.


Twitter This Tip! ReTweet this Tip!