PowerShell to gather Lenovo Warranty information

Lenovo Warranty Information Lookup

To continue with the theme of my last post, I’m going to show how to gather the warranty information for Lenovo Servers.  This was a bit tricky as this isn’t an API call, but more of a website parsing.

Getting the Lenovo Server information

To be able to perform this warranty lookup, the server information will need to be retrieved, and what a better way than getting it from the VMhosts themselves.

$Manufacturer = @()
$Clusters = Get-Cluster
Foreach ($cluster in $clusters){
    $VMhosts = $cluster | Get-vmhost
        foreach ($VMhost in $VMhosts){
            $line              = "" | Select VMhost, Manufacturer
            $line.VMhost       = $VMhost.name
            $line.Manufacturer = $VMhost.Manufacturer
            $Manufacturer     += $line
        }
}
$Manufacturer | Sort-Object -Property Manufacturer

This gathers all of the Manufacturer information of all of the VMhosts within the clusters in vSphere. So now you can either just target the VMhosts or clusters that contain the Lenovo hardware.

Getting the serial number from the Host

This is a simple one liner that gets the serial number

$cli = Get-vmhost $VMhost | Get-EsxCli
$serialno = $cli.hardware.platform.get().serialnumber

Get model information to query the warranty site

I don’t normally use regular expression, just because the syntax of it is just confusing.  So i have been forcing myself to make use of it more often as it can help to save line of code.


$Manufacturer = @()
$Clusters = Get-Cluster
Foreach ($cluster in $clusters){
    $VMhosts = $cluster | Get-vmhost
        foreach ($VMhost in $VMhosts){
            $line                    = "" | Select VMhost, Manufacturer, Model, Class, MachineType, ShortModel
            [regex]$regexClass       = "\d{4}"
            [regex]$regexMachType    = "\d{4}[A-Z][A-Z]\d{1}"
            [regex]$regexModel       = "[x]\d{3}\s[A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z]\s[A-Za-z][A-Za-z][A-Za-z][A-Za-z]"
            $line.VMhost             = $VMhost.name
            $line.Manufacturer       = $VMhost.Manufacturer
            $line.model              = $vmhost.Model
            $line.Class              = ($regexClass.Matches($($vmhost.model))).Value
            $line.MachineType        = ($regexMachType.Matches($($vmhost.model))).Value
            $line.ShortModel         = ((($regexModel.Matches($($vmhost.model))).Value).replace(" ","-")).ToLower()
            $Manufacturer           += $line
              
        }
}
$Manufacturer | Sort-Object -Property Manufacturer

This updated code will pull the required information to be able to put the URL together to query for the warranty information.  The regex code is based on the models that my environment has which are Flex System x440 Compute Node -[7917AC1]- and Flex System x240 Compute Node -[8737AC1]-.  You mileage may differ.

Querying for the warranty dates

The format for the URL that has the warranty information is as follows.

https://datacentersupport.lenovo.com/us/en/products/servers/flex/$($shortmodel)/$($class)/$($machinetype)/$($serialno)/warranty

As you can see we have all of the information gather via the previous code snippets.  Now we just need to put it all together to make the proper URL, invoke the web request and parse the content for the information we are looking for.

$Manufacturer = @()
$Clusters = Get-Cluster kyvmc08
Foreach ($cluster in $clusters){
    $VMhosts = $cluster | Get-vmhost
        foreach ($VMhost in $VMhosts){
            $line                    = "" | Select VMhost, Manufacturer, SerialNo, Model, Class, MachineType, ShortModel, WarrantyStart, WarrantyEnd
            $cli = Get-vmhost $VMhost | Get-EsxCli
            $serialno = $cli.hardware.platform.get().serialnumber
            [regex]$regexClass       = "\d{4}"
            [regex]$regexMachType    = "\d{4}[A-Z][A-Z]\d{1}"
            [regex]$regexModel       = "[x]\d{3}\s[A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z][A-Za-z]\s[A-Za-z][A-Za-z][A-Za-z][A-Za-z]"
            $shortmodel              = ((($regexModel.Matches($($vmhost.model))).Value).replace(" ","-")).ToLower()
            $class                   = ($regexClass.Matches($($vmhost.model))).Value
            $machinetype             = ($regexMachType.Matches($($vmhost.model))).Value
            $url = "https://datacentersupport.lenovo.com/us/en/products/servers/flex/$($shortmodel)/$($class)/$($machinetype)/$($serialno)/warranty"
            $WebRequestResult = Invoke-WebRequest -Uri $URL
            $date = (($WebRequestResult.AllElements | Select innerHTML | Select-String -Pattern $serialno) | Select -Skip 1 | Select -First 1) | Select-String "\d{4}-\d{2}-\d{2}"
            [regex]$regex = "\d{4}-\d{2}-\d{2}"
            $warrantydates = $regex.Matches($date) | Foreach {$_.value} | Select -Skip 1 | Select -First 2
            $WarrantyStart = $warrantydates[1]
            $WarrantyEnd = $warrantydates[0]
            $line.VMhost             = $VMhost.name
            $line.Manufacturer       = $VMhost.Manufacturer
            $line.SerialNo           = $serialno
            $line.model              = $vmhost.Model
            $line.Class              = $class
            $line.MachineType        = $machinetype
            $line.ShortModel         = $shortmodel
            $line.WarrantyStart      = $WarrantyStart
            $line.WarrantyEnd        = $WarrantyEnd
            $Manufacturer           += $line
              
        }
}
$Manufacturer | Sort-Object -Property Manufacturer | Ft -AutoSize

Below a sample of the output from the script.

Results of the Lenovo Warranty Script

– Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.