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("")+6)] $htData = $htData -replace '(?sx:)' $htData = $htData -replace '(?sx:)' $htData = $htData -replace ' & ', " and " $htData = $htData -replace "&\w{2,6};" <# will parse without these cleanup steps $htData = $htdata -replace '^\s+$', " " $htData = $htData -replace '' $htData = $htData -replace '(?sx:)' $htData = $htData -replace '(?sx:)' $htData = $htData -replace '(?sx:)' 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