Powershell Script and web query – Lenovo Think Model Name (ex. 20AMxxx) to Family (ex. X240)

13 02 2014

Tiny bit of background info about this Lenovo challenge:
Model is the long number reported to the OS (ex. 2324) and Family is the product / family / generation name (ex. X230) which really what I work with in my workplace, i needed to have a an easy way of querying Lenovo website to get the Family based on the Type / Model.


This script will process a CSV with Model Column and model numbers underneath it, it was tested on Powershell 3.0.
The script checks for the 1st four (4) characters in Lenovo website and skips past queried model (per-script-run) with the exact 4 characters (makes it run much faster and saves the filtering in the CSV manually by you).

Later i will add more info about the tutorial part of this along with lessons learned from writing this script.
As always comments will be appreciated :)

Progress will look something like this:

Query Result for Model: 2324 Returned following Family: ThinkPad X230 (2324) ThinkPad X230i (2324)
Query Result for Model: 7673 Returned following Family: ThinkPad X61 (7673)
Query Result for Model: 6466 Returned following Family: ThinkPad T61 (6466)
Query Result for Model: 4290 Returned following Family: ThinkPad X220 (4290) ThinkPad X220i (4290)
Query Result for Model: 7449 Returned following Family: ThinkPad X200 Tablet (7449)
Skipping previously queried model: 2324

Full Code is Here:

# Author Avi Elmaliah
# 1. Create CSV file with models (20AMxxx or 2324cto etc.) in the column called "Model" - call it laptops.csv (changeable below)
# 2. Place the CSV file in the same folder of the script
# 3. Run the script another CSV will be created with the name results.csv
#checks powershell version >= 3
if ($host.Version.Major -lt "3")
 write-host -ForegroundColor Red $host.Version.Major "this script doesn't work well with powershell 2 and has been tested with powershell 3"
 Read-Host "press enter to exit"
#gets the folder where powershell script is invoked from
$currentPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
#change the name to match
$CSV = Import-Csv -Path "$($currentPath)\laptops.csv"
$Result = @()
$queriedmodels = @() #allows speeding up the script and not querying the web for models already queried
foreach ($line in $CSV)
 if ($line.Model -ne "")
 $model = $line.Model.Substring(0,4) #get the first 4 letters of the model those are what's important for the query
 if ($queriedmodels.Contains("$model")) #allows speeding up the script and not querying the web for models already queried
 Write-Host "Skipping previously queried model: " $model
 [XML]$querymodel = (New-Object System.Net.WebClient).DownloadString("http://support.lenovo.com/en_IL/downloads/default/1341290814874.ajax?method=getQuickPath&QuickPathSearch=$model")
 $family = [string]::Join(";",($querymodel.GetElementsByTagName("Product")).name)
 Add-Member -InputObject $line -MemberType NoteProperty -Name Family -Value $Family -Force
 Write-Host "Query Result for Model:" $model " Returned following Family: " $family
 $queriedmodels += $model
 $Result += $line

#$Result | Out-GridView -title "Please Review the Results. Click on Ok to save to file" -PassThru
$Result_Csv = $Result | ConvertTo-Csv -NoTypeInformation
$Result_Csv | Out-File -Force -FilePath "$currentPath\results.csv"



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: