Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returns driver pack info for HP workstations by scraping the HP driver packs web pages.
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false)]
[ValidateSet('64-bit','32-bit')]
$Architecture = "64-bit"
)
# Load the HP driver pack web page
If ($Architecture -eq "64-bit")
{
$URI = "https://ftp.hp.com/pub/caps-softpaq/cmit/HP_Driverpack_Matrix_x64.html"
}
If ($Architecture -eq "32-bit")
{
$URI = "https://ftp.hp.com/pub/caps-softpaq/cmit/HP_Driverpack_Matrix_x86.html"
}
$HTML = Invoke-WebRequest -Uri $URI -ErrorAction Stop
# Scrape the table rows and headers for the datatable
$TableRows = $HTML.AllElements | where {$_.tagName -eq "TR"-and $_.innerHTML -match "<P>"}
$Headers = ($HTML.AllElements | where {$_.tagName -eq "TR" -and $_.innerHTML -match "Windows 10"})[0].innerHTML
[string]$ColumnExpression = "<td[^>]*>(.*?)</td>"
$HeaderMatches = [Regex]::Matches($Headers, $ColumnExpression, ([System.Text.RegularExpressions.RegexOptions]::Multiline, [System.Text.RegularExpressions.RegexOptions]::Singleline, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase))
# Load the headers as columns into a datatable
$DataTable = New-Object System.Data.DataTable
[void]$DataTable.Columns.Add("Model")
foreach ($Match in ($HeaderMatches | Select -Skip 1))
{
[void]$DataTable.Columns.Add($Match.Value.Split('>')[1].Replace('</TD',''))
}
foreach ($Table in $TableRows)
{
[string]$ColumnExpression = "<td[^>]*>(.*?)</td>"
$Matches = [Regex]::Matches($Table.innerHTML, $ColumnExpression, ([System.Text.RegularExpressions.RegexOptions]::Multiline, [System.Text.RegularExpressions.RegexOptions]::Singleline, [System.Text.RegularExpressions.RegexOptions]::IgnoreCase))
$array = New-Object System.Collections.ArrayList
# Process each regex match (table data entry)
foreach ($Item in $Matches)
{
# Add the models
If ($Item.Index -eq 0)
{
[array]$Models = ($Item.Value.Split('>') | where {$_ -match "/p"}).Replace("</P","")
foreach ($Model in $Models)
{
[string]$ModelString = $ModelString + "$Model, "
}
$ModelString = $ModelString.TrimEnd(', ')
[void]$array.Add($ModelString)
Remove-Variable -Name ModelString -Force
}
# Add the driver pack url and version
Else
{
If ($Item.Value -notmatch "<P>")
{
$Value = "-"
}
Else
{
$href = $Item.Value.Split() | Where {$_ -match "href"} | Where {$_ -match "exe"}
$Link = $href.Split('>')[0].Replace('href="','').TrimEnd('"')
$Version = ($Item.Value.Split('=') | where {$_ -match "Version"}).Replace('"','').TrimEnd(' href')
$Value = "$Link ($Version)"
}
[void]$array.Add($Value)
}
}
# Add an entry to the datatable
If ($Architecture -eq '64-bit')
{
[void]$DataTable.Rows.Add($array[0],$array[1],$array[2],$array[3],$array[4],$array[5],$array[6],$array[7],$array[8],$array[9],$array[10],$array[11])
}
If ($Architecture -eq '32-bit')
{
[void]$DataTable.Rows.Add($array[0],$array[1],$array[2],$array[3],$array[4],$array[5],$array[6])
}
}
Return $DataTable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.