Find HP machine age from powershell script

Collects warranty start date from HP warranty website

Link to the script: find_hp_warranty

Powershell:


$sn = "CZC12068T4"
$country = "DK"

$body = "rows%5B0%5D.item.serialNumber=" + $sn + "&rows%5B0%5D.item.countryCode=" + $country + "&rows%5B1%5D.item.serialNumber=&rows%5B1%5D.item.countryCode=US&rows%5B2%5D.item.serialNumber=&rows%5B2%5D.item.countryCode=US&rows%5B3%5D.item.serialNumber=&rows%5B3%5D.item.countryCode=US&rows%5B4%5D.item.serialNumber=&rows%5B4%5D.item.countryCode=US&rows%5B5%5D.item.serialNumber=&rows%5B5%5D.item.countryCode=US&rows%5B6%5D.item.serialNumber=&rows%5B6%5D.item.countryCode=US&rows%5B7%5D.item.serialNumber=&rows%5B7%5D.item.countryCode=US&rows%5B8%5D.item.serialNumber=&rows%5B8%5D.item.countryCode=US&rows%5B9%5D.item.serialNumber=&rows%5B9%5D.item.countryCode=US&submitButton=Submit"

$r=Invoke-WebRequest -Uri ("http://h20564.www2.hp.com/hpsc/wc/public/find") -Method POST -Body $body

$ss = $r.RawContent.IndexOf("Wty: HP Support for Initial Setup")

# Extra run, in case of not found. 
if($ss -lt 100) {
$r=Invoke-WebRequest -Uri ("http://h20564.www2.hp.com/hpsc/wc/public/find") -Method POST -Body $body
$ss = $r.RawContent.IndexOf("Wty: HP Support for Initial Setup")
}

if($ss -gt 100) {
$result = $r.rawcontent.substring($ss+42,20)
$end = $result.IndexOf("</")
$result = $result.Substring(0,$end)
get-date -Date $result -UFormat "%Y/%m/%d"
}

else {
$result = "not found"
$result
}