[PowerShell] / Get-HpProductInfo.ps1 Repository:
ViewVC logotype

View of /Get-HpProductInfo.ps1

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (download) (annotate)
Mon Aug 19 01:55:52 2013 UTC (10 years, 8 months ago) by ian
File size: 13324 byte(s)
script is now provides full base functionality (product info, warranty, parts);
þÿ##################################################

#

#  Written by Ian Cammarata

#  Get-HpProductInfo.ps1 v0.1

#

##################################################

#

#  Usage:

#    Get-HpProductInfo < serial number >

#  Ex:

#    Get-HpProductInfo JPBCD3R1H3

#

##################################################

#

#  Notes:

#    -Will probably only work for products sold in the US unless you change the country

#     codes in $hpWarrantyQueryURL and the $htClient.Headers.set(...Cookie...

#

#  To Do:

#    -Make it a proper PowerShell script (receive piped input, input validation, multiple SNs at once)

#    -Internally place data into an object so it can be exported to an XML cache for

#     faster loading if the same SN is queried again.

#    -Multi-threading the http downloads and multiple retry attemps (partsufer is so unreliable lately)

#    -Option to filter parts output

#

##################################################



Set-StrictMode -Version Latest

$ErrorActionPreference = "Stop" #Needed so script won't continue if the web server returns an error



$lookupSn = "CND120CJH0"

if ($args) { $lookupSn = $args[0] }



#SNs to test with CND120CJH0, CNU305BP9N, CNFKF52058, JPBCD3R1H3



$partSurferQueryUrl = "http://partsurfer.hp.com/Search.aspx?searchText="

#http://h20000.www2.hp.com/bizsupport/TechSupport/WarrantyResults.jsp?lang=en&cc=us&prodSeriesId=0&prodTypeId=0&sn=CND120CJH0&pn=SJ998UC&country=US&nickname=&find=Display+Warranty+Information+%C2%BB

$hpWarrantyQueryUrl = "http://h20000.www2.hp.com/bizsupport/TechSupport/WarrantyResults.jsp?lang=en&cc=us&country=US&find=Display+Warranty+Information+%C2%BB"

#&sn=CND120CJH0&pn=SJ998UC



$htClient = New-Object System.Net.Webclient

#Cookie ||| Name: country - Content: United%20States - host: partsufer.hp.com - Path: /

$htClient.Headers.Set([System.Net.HttpRequestHeader]::Cookie, "Country=United%20States")



$htData = $htClient.DownloadString("$($partSurferQueryUrl)$($lookupSn)")



#Clean HTML so it can be parsed into XML

$htData = -join $htData[$htData.IndexOf("<body")..($htData.LastIndexOf("</body>")+6)]

$htData = $htData -replace '(?sx:<script[\s\S]*?</script[\s\S]*?>)'

$htData = $htData -replace '(?sx:<noscript[\s\S]*?</noscript[\s\S]*?>)'

$htData = $htData -replace ' & ', " and "

$htData = $htData -replace "&\w{2,6};"

<#  will parse without these cleanup steps

cleaning for warranty:

$htData = $htData -replace " (\w+)=([a-zA-Z0-9]+)", ' $1="$2"'

$htData = $htData -replace " nowrap"

$htData = $htData -replace "<a .*?</a>"

$htData = $htData -replace "<img [^>]*>"



#cleaning for partsurfer

$htData = $htdata -replace '^\s+$', " "

$htData = $htData -replace '<!Doctype[\s\S]*?>'

$htData = $htData -replace '(?sx:<option[\s\S]*?</option[\s\S]*?>)'

$htData = $htData -replace '(?sx:<!--[\s\S]*?-->)'

$htData = $htData -replace '(?sx:<input[\s\S]*?/>)'

end unused cleanup #>

$partSurferXml = [xml]$htData



$ErrorActionPreference = "SilentlyContinue" #Needed so filtering by ID won't generate an error on nodes with no ID property

#Parse SN, Sku, and Description from XML

$skuXmlObjects = $partSurferXml.SelectNodes("//span") | ? { $_.id -like "*lbl*" -and $_.id -notlike "*BOM*" -and $_.id -notlike "*text*" }

#Parse Parts from XML

$partXmlObjects = $partSurferXml.SelectNodes("//span") | ? { $_.id -like "*BOM*part*" -and $_.'#text' -notlike "*N/A*" }

#Get collection of input:checkboxes to match against to determine if part is orderable

$orderCheckboxXmlObjects = $partSurferXml.SelectNodes("//input") | ? { $_.id -like "*chkSpareBOM" }

$ErrorActionPreference = "Continue"



$serialNumber = ($skuXmlObjects | ?{$_.id -like "*SerialNumber"}).'#text'

$productNumber = ($skuXmlObjects | ?{$_.id -like "*ProductNumber"}).'#text'

$productDescr = ($skuXmlObjects | ?{$_.id -like "*Description"}).'#text'



#Parse orderable U.S. parts from XML

$orderableParts = @()

foreach ( $part in ( $partXmlObjects | ? { $_.id -like "*lblspart1" } ) ) {

    if ( $part.id -match ".*(_ctl\d{2,3}_).*" ) {

        if ( $orderCheckboxXmlObjects | ? { $_.id -like "*$($matches[1])*" } ) {

        $newPart = ($partXmlObjects | ? { $_.id -like "*$($matches[1])*" -and $_.id -notlike "*Enhanced" } | % { $_.'#text' }) -join "`t"

        #$partText = $partXmlObjects | ? { $_.id -like "*$($matches[1])*" -and $_.id -notlike "*Enhanced" } | % { $_.'#text' }   

        #$newPart = $partText -join "`t"

        #if( $newPart -like "*-001*" ) { $orderableParts += $newPart } #This works for computers but returns no results for printers

        $orderableParts += $newPart

        }

    }

}



#Query Warranty ### This page is too messy to be cleaned and parsed, need to just scrape with regex

$ErrorActionPreference = "Stop" #Needed so script won't continue if the web server returns an error

$htData = $htClient.DownloadString("$($hpWarrantyQueryUrl)&sn=$($serialNumber)&pn=$($productNumber)")

$ErrorActionPreference = "Continue"



$warranty = @()

$htData = $htData -replace "<br>"

while ($htData -match "<td[^>]*>\s*([\w\s:]*)\s*</td>\s*<td[^>]*>\s*(\d{1,2} \w{3} \d{4})\s*</td>\s*<td[^>]*>\s*(\d{1,2} \w{3} \d{4})\s*</td>" ) {

    $warranty += $Matches[1..3]

    $htData = $htData.Replace($Matches[0],1)

}

$warranty = $warranty -replace "wty: |HWM |HP |HW |Maintenance |Support |for "

$warranty = $warranty -replace "Onsite", "Onsite        " #This can be deleted later once the output display code is more sophisticated

<###  way too messy to clean even a small section of the warranty page for the XML parser

$htData -match "<table.*\n.*Add summary of the data table[\S\s]*?</table>"

$tmp = $matches[0]

$tmp = $tmp -replace " (\w+)=([a-zA-Z0-9#%]+)", ' $1="$2"'

$tmp = $tmp -replace " nowrap"

$tmp = $tmp -replace "<a .*?</a>"

$tmp = $tmp -replace "<img [^>]*>"

$tmp = $tmp -replace "&\w{2,6};"

$tmp = $tmp -replace "<br>", "<br/>"

$tmp = $tmp -replace ' & ', " and "

#Make all HTML tags lowercase since the XML parser generates errors if the opening and closing tags aren't the same case

while ( $tmp -cmatch '(</?[A-Z]+)' ) {

  $newTxt = $Matches[1].ToLower()

  write-debug "Replacing all occurances of ""$($Matches[1])"" with ""$($newTxt)"""

  $tmp = $tmp -creplace $Matches[1], $newTxt

}

###>



#Display data

"-"*90



"Serial Number:`t$($serialNumber)"

"Product Number:`t$($productNumber)"

"Description:`t$($productDescr)"



"-"*90



"Warranty:"

$i = 0

while ( $i -lt $warranty.Length ){

  "$($warranty[$i])`t`tStart: $($warranty[$i+1])`t`tEnd: $($warranty[$i+2])"

  $i += 3

}



"-"*90



"Available Parts: $($orderableParts.Length)"

"-"*90

$orderableParts

"-"*90

Contact
ViewVC Help
Powered by ViewVC 1.0.4