<# .NAME WarrantyInfo Userscript .ABOUT Script collects warranty information from the computer that it is run on and then adds the information to WMI so it can be collected by SCCM .INFO Copyright Ryan McLean 2011 ryan1 underscore 00 at hotmail antispam dot co dot uk .VERSION 0.3 .FIXES - 0.3 - Fixed call to GetHPDaysLeft which was passing 2 params instead of 1 - 0.2 - Fixed Warranties starting in the future placing extra tags around the daysleft #> # 2/3 - WarrantyInfo Scripts ############## User Configureable ############### $warrantyInfoDir = "c:\warrantyinfo" $outfile = $warrantyInfoDir + "\WarrantyInfo.csv" $logfile = "WarrantyInfo.log" # Not yet implemented. $enableHP = $true # HP Warranties have not been fully tested # Debugging Options $enablelogging # Not yet implemented. $enableDebug = $false # Prints out contents of some variables to console $enableTrace = $false # Enable console output showing which function you are in $enableVendorForce = $false $ForcedManufacturer = "HP" # Valid Manufacturers "HP" | "DELL" $ForcedTag = "12341251" # 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 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 GetTagNumber { if($enabletrace) {write-host "GetTagNumber"} try { $ErrorActionPreference = "Stop"; #Make all errors terminating $sn = (Get-WmiObject -Class Win32_BIOS).SerialNumber $man = (Get-WmiObject -Class Win32_BIOS).Manufacturer } Catch { Write-Host "An Error has occured connecting to the system"; Cleanup exit(1) } return $sn, $man } 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) 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.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) 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.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"} if($enableVendorForce) { write-host "Forcing Vendor" $manu = $ForcedManufacturer $tag = $ForcedTag } else { $tag, $manu = GetTagNumber if($enabletrace) {write-host "Main"} } if($enableDebug) {$manu} #Get Webpage if ($manu -like "*Dell*") { if($enabletrace) {write-host "Dell GetWebpage"} $DellURL += $tag if($enableDebug) {$DellURL} $content = (new-object net.webclient).DownloadString($DellURL) } elseif ((($manu -eq "HP") -or ($manu -eq "Hewlett-Packard")) -and ($enableHP -eq $true)) { if($enabletrace) {write-host "HP GetWebpage"} $HPURL += $tag if($enableDebug) {$HPURL} $content = (new-object net.webclient).DownloadString($HPURL) } 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 - GetHPContractsTable"} $contractsTable = GetDellContractsTable $content if($enableDebug) {$contractsTable} if($enabletrace) {write-host "Main - Dell - GetDellContracts"} $contracts = GetDellContracts $contractsTable 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 if($enabletrace) {write-host "Main"} } else { # Should never get executed but ... Write-Host "An Error has occured"; Cleanup exit(1) } if($enableDebug) {$contracts} $contracts | Export-Csv $outfile Cleanup } ################### MAIN ###################### $runtime = Get-Date -UFormat "%Y-%m-%d %H:%M:%S" Main;