forked from dfinke/ImportExcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-HtmlTable.ps1
42 lines (33 loc) · 1.16 KB
/
Get-HtmlTable.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 {$_.innertext -replace ' ',''})
} else {
$propertyNames = @(1..($cells.Count + 2) | % { "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
}
}