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

View of /Get-HpProductInfo.ps1

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (download) (annotate)
Sat Aug 17 19:39:04 2013 UTC (10 years, 8 months ago) by ian
File size: 5496 byte(s)
Eliminated many lines, improved readability, and speed by using silent erroraction when filting nodes by id instead of several lines of error checking;
New more robust method of iterating through parts;
þÿ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



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

$htClient = New-Object System.Net.Webclient



$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

$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 SN, Sku, and Description

$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") |

<# | ? {

    if( $_.PSObject.Properties.Match('id').count ){

        if( $_.id -like "*_chkSpareBOM" ) { 1 }

    }

}#>



$ErrorActionPreference = "Continue"



#Parse orderable U.S. parts from XML

$orderableParts = @()

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

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

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

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

        $newPart = $partText -join "`t"

        if( $newPart -like "*-001*" ) { $orderableParts += $newPart }

    }

}



#Display data

"-"*90

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

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

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

""

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

"-"*90

$orderableParts

"-"*90

Contact
ViewVC Help
Powered by ViewVC 1.0.4