mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
42 lines
1.2 KiB
PowerShell
42 lines
1.2 KiB
PowerShell
function Get-HtmlTable {
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
$url,
|
|
$tableIndex=0,
|
|
$Header,
|
|
[int]$FirstDataRow=0,
|
|
[Switch]$UseDefaultCredentials
|
|
)
|
|
|
|
$r = Invoke-WebRequest $url -UseDefaultCredentials: $UseDefaultCredentials
|
|
|
|
$table = $r.ParsedHtml.getElementsByTagName("table")[$tableIndex]
|
|
$propertyNames=$Header
|
|
$totalRows=@($table.rows).count
|
|
|
|
for ($idx = $FirstDataRow; $idx -lt $totalRows; $idx++) {
|
|
|
|
$row = $table.rows[$idx]
|
|
$cells = @($row.cells)
|
|
|
|
if(!$propertyNames) {
|
|
if($cells[0].tagName -eq 'th') {
|
|
$propertyNames = @($cells | ForEach-Object {$_.innertext -replace ' ',''})
|
|
} else {
|
|
$propertyNames = @(1..($cells.Count + 2) | Foreach-Object { "P$_" })
|
|
}
|
|
continue
|
|
}
|
|
|
|
$result = [ordered]@{}
|
|
|
|
for($counter = 0; $counter -lt $cells.Count; $counter++) {
|
|
$propertyName = $propertyNames[$counter]
|
|
|
|
if(!$propertyName) { $propertyName= '[missing]'}
|
|
$result.$propertyName= $cells[$counter].InnerText
|
|
}
|
|
|
|
[PSCustomObject]$result
|
|
}
|
|
} |