In the previous example we illustrated how you can scrape Lenovo driver information from the web. In this example, some of the information returned was raw numeric information: the number “3” represented “Reboot required”, for example.

In this tip we’d like to show how you can translate cryptic numeric values to friendly text. First, let’s take a look at the improved Lenovo function:

function Get-LenovoDriver
{
     param
     (
          $Model = '20JN',
          
          $Os = 'Win7',
          
          $Category = '*'
     )
     
     
     $restartText = @{
          '0' = "No restart"
          '1' = "Forced restart"
          '3' = "Restart required"
          '4' = "Shutdown after install"
     }
     
     $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
               $readme = [System.IO.Path]::ChangeExtension($filename, 'txt')
               $readme = $info.Package.Files.Readme.file.Name
               
               [PSCustomObject]@{
                    Category = $name
                    Command = $info.package.ExtractCommand
                    Space = $info.package.DiskspaceNeeded
                    SpaceMB = [Math]::Round( ($info.package.DiskspaceNeeded / 1MB), 1)
                    Reboot = $info.package.Reboot.type
                    RebootFriendly = $restartText[$info.package.Reboot.type]
                    Version = $info.package.version
                    Download = "https://download.lenovo.com/pccbbs/mobiles/$filename"
                    ReadMe = "https://download.lenovo.com/pccbbs/mobiles/$readme"
                    Datum = Get-Date
               }
          }
}

You can now get information about any driver update for any Lenovo machine simply like this:

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


Category       : Camera and Card Reader
Command        : n1qib04w.exe /VERYSILENT /DIR=%PACKAGEPATH% /EXTRACT="YES"
Space          : 6442356
SpaceMB        : 6,1
Reboot         : 1
RebootFriendly : Forced restart
Version        : 3760
Download       : https://download.lenovo.com/pccbbs/mobiles/n1qib04w.exe
ReadMe         : https://download.lenovo.com/pccbbs/mobiles/n1qib04w.txt
Datum          : 09.12.2021 13:21:21   
 

The property “Reboot” shows the original cryptic numeric value. The new property “RebootFriendly” shows the friendly text representation, in this case “Forced restart”.

Let’s look at the source code to understand the translation.

For any translation you need a hash table that associates the cryptic numbers with the friendly text:

$restartText = @{
          '0' = "No restart"
          '1' = "Forced restart"
          '3' = "Restart required"
          '4' = "Shutdown after install"
     }

Next, translating numeric values to text is as simple as looking up hash table values:

...
Reboot = $info.package.Reboot.type
RebootFriendly = $restartText[$info.package.Reboot.type]
...




Twitter This Tip! ReTweet this Tip!

Anonymous