<# .NAME WarrantyInfoFromFile .ABOUT Script collects warranty information from the Dell/HP websites using a csv list of computers and their tags .INFO Copyright Ryan McLean 2011 ryan1 underscore 00 at hotmail antispam dot co dot uk .VERSION 0.1 .NOTES CSV in file MUST have the following headers "Name,Vendor,Tag" E.g. "Computer1,DELL,12345" The Headers are the important part and must exist for this script to work. #> ############## User Configureable ############### $infile = "\tagsin.csv" $outfile = "\tagsout.csv" $enableHP = $true # HP Warranties have not been fully tested # Debugging Options $enableDebug = $false # Prints out contents of some variables to console $enableTrace = $false # Enable console output showing which function you are in #### # Vendor Websites - Change only if Dell/HP change $DellURL = "http://support.dell.com/support/topics/global.aspx/support/my_systems_info/details?c=us&l=en&s=gen&~tab=1&ServiceTag=" $HPURL = "http://h20000.www2.hp.com/bizsupport/TechSupport/WarrantyResults.jsp?country=US&sn=" # Vendor Date patterns, here in case they change but should be left alone $DellDate = "M/d/yyyy" $HPDate = "dd MMM yyyy" ####### Class Definations ######## #Create Contract Class Add-Type @' public class Contract { public string Name = ""; public string Description = ""; public string Provider = ""; public string StartDate = ""; public string EndDate = ""; public string DaysLeft = ""; public string Status = ""; } '@ ############## Functions ########################## Function Cleanup { if($enabletrace) {write-host "Cleanup"} try { $ErrorActionPreference = "SilentlyContinue"; Remove-Variable -Scope Global * } catch { } finally { $ErrorActionPreference = "Continue"; } } Function NormalizeDates { param ($rawDate, $dformat) if($enabletrace) {write-host "NormalizeDates"} $tDate = [datetime]::ParseExact($rawDate, $dformat, $null) if($enableDebug) { write-host $tDate } return get-date $tDate -UFormat "%Y-%m-%d" } Function GetDellContractsTable { param ($rawhtml) if($enabletrace) {write-host "GetDellContractsTable"} #Get Contracts Table $rawhtml -match '(?s)]+class="contract_table">(.*?)' return $matches[1] } Function GetDellContracts { param ($table, $name) if($enabletrace) {write-host "GetDellContracts"} #Seperate out Contracts and headers $pattern = '.*?(.*?)' $res = ([regex]::matches($table, $pattern) | %{$_.value}) $x = 0 $rawcontracts = (1..($res.length-1)) foreach ($item in $res) { if ($x -ne 0) { $rawcontracts[($x-1)] = $item } $x++ } #Seperate Out Contracts $contracts = (1..$rawcontracts.length) $pattern = ']*>>?(.*?)' $ptrnTagRem = '<(.|\n)*?>' $x = 0 foreach ($rawcontract in $rawcontracts) { $Contract = New-Object Contract $Contract.Name = $name $Contract.Description,$Contract.Provider,$Contract.StartDate,$Contract.EndDate,$Contract.DaysLeft = ` ([regex]::matches($rawcontract, $pattern) | %{$_.Groups[1].value}) # Clean Days left incase of extra tags $Contract.DaysLeft = $Contract.DaysLeft -replace $ptrnTagRem, "" if ( $Contract.DaysLeft -gt '0' ) { $Contract.Status = "Active" } else { $Contract.Status = "Expired" } $Contract.StartDate = NormalizeDates $Contract.StartDate $DellDate $Contract.EndDate = NormalizeDates $Contract.EndDate $DellDate $Contract.Description = $Contract.Description -replace $ptrnTagRem, "" $contracts[$x] = $Contract $x++ } return $contracts } Function GetHPContractsTable { param ($rawhtml) if($enabletrace) {write-host "GetHPContractsTable"} #Get Contracts Table $rawhtml -match '(?s)]+summary.*?>(.*?)' $ctable = $matches[1] $ctable = $ctable -replace '\s+',' ' return $ctable } Function GetHPDaysLeft { param ($eDate) if($enabletrace) {write-host "GetHPDaysLeft"} $daysleft = ([datetime]$eDate - [datetime]::today).days if ($daysleft -le '0') { return 0 } else { return $daysleft } } Function GetHPContracts { param ($table, $name) if($enabletrace) {write-host "GetHPContracts"} #Seperate out Contracts and headers $pattern = '.*?]*.*?>(.*?)' $res = ([regex]::matches($table, $pattern) | %{$_.value}) $x = 0 $rawcontracts = (1..($res.length-1)) foreach ($item in $res) { if ($x -ne 0) { $rawcontracts[($x-1)] = $item } $x++ } #Seperate Out Contracts $contracts = (1..$rawcontracts.length) # Get rid of Warranty Type $rawcontracts = $rawcontracts -replace ']+rowspan.*?>.*?', "" $pattern = ']*>>?(.*?)' $ptrnTagRem = '<(.|\n)*?>' $x = 0 foreach ($rawcontract in $rawcontracts) { $Contract = New-Object Contract $Contract.Name = $name $Contract.Description,$Contract.StartDate,$Contract.EndDate,$Contract.Status,$rubbish = ` ([regex]::matches($rawcontract, $pattern) | %{$_.Groups[1].value}) $Contract.StartDate = NormalizeDates $Contract.StartDate $HPDate $Contract.EndDate = NormalizeDates $Contract.EndDate $HPDate $Contract.DaysLeft = GetHPDaysLeft $Contract.EndDate #Count days left $Contract.Provider = "HP" # Strip Tags $Contract.Description = $Contract.Description -replace $ptrnTagRem, "" $Contract.Description = $Contract.Description -replace '^\s+', "" $Contract.Description = $Contract.Description -replace 'Wty:\s+HP\s+', "" $Contract.Status = $Contract.Status -replace $ptrnTagRem, "" $contracts[$x] = $Contract $x++ } return $contracts } Function Main { if($enabletrace) {write-host "Main"} $output = @() $csvfile = Import-Csv $infile foreach ($entry in $csvfile) { if($enabletrace) {write-host "Main - Loop"} $tag = $entry.Tag $manu = $entry.Vendor $name = $entry.Name $tag, $manu, $name = GetTagNumber $entry if($enabletrace) {write-host "Main"} if($enableDebug) {$manu} #Get Webpage if ($manu -like "*Dell*") { if($enabletrace) {write-host "Dell GetWebpage"} $URL = $DellURL $URL += $tag if($enableDebug) {$URL} $content = (new-object net.webclient).DownloadString($URL) } elseif ((($manu -eq "HP") -or ($manu -eq "Hewlett-Packard")) -and ($enableHP -eq $true)) { if($enabletrace) {write-host "HP GetWebpage"} $URL = $HPURL $URL += $tag if($enableDebug) {$URL} $content = (new-object net.webclient).DownloadString($URL) } else { # Should never get executed but ... Write-Host "An Error has occured"; Cleanup exit(1) } if($enableDebug) {$content} # Parse html if ($manu -like "*Dell*") { if($enabletrace) {write-host "Main - Dell - GetDellContractsTable"} $contractsTable = GetDellContractsTable $content if($enableDebug) {$contractsTable} if($enabletrace) {write-host "Main - Dell - GetDellContracts"} $contracts = GetDellContracts $contractsTable $name if($enabletrace) {write-host "Main"} } elseif ((($manu -eq "HP") -or ($manu -eq "Hewlett-Packard")) -and ($enableHP -eq $true)) { if($enabletrace) {write-host "Main - HP - GetHPContractsTable"} $contractsTable = GetHPContractsTable $content if($enableDebug) {$contractsTable} if($enabletrace) {write-host "Main - HP - GetHPContracts"} $contracts = GetHPContracts $contractsTable $name if($enabletrace) {write-host "Main"} } else { # Should never get executed but ... Write-Host "An Error has occured"; Cleanup exit(1) } $output = $output + $contracts } if($enableDebug) {$contracts} if($enabletrace) {write-host "Exporting...."} $output | Export-Csv $outfile -NoTypeInformation Cleanup } ################### MAIN ###################### $runtime = Get-Date -UFormat "%Y-%m-%d %H:%M:%S" Main;