-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvertFrom-HTMLTable.ps1
57 lines (49 loc) · 1.65 KB
/
ConvertFrom-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Function ConvertFrom-HTMLTable {
<#
.SYNOPSIS
Convert HTML tables to Powershell objects
.DESCRIPTION
Scrapes HTML from web site, and parses elements of table, converting each line into a PSCustomObject
.PARAMETER Name
Uri
.EXAMPLE
ConvertFrom-HTMLTable -Uri 'www.webpage.com'
.Notes
Based on code from 'Daniel Srlv'.
http://poshcode.org/3664
#>
[CmdletBinding()]
[OutputType('System.PSCustomObject')]
Param (
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[uri]$Uri
)
Process {
$WebResponse = Invoke-WebRequest -Uri $Uri
$HTMl = $WebResponse.ParsedHtml
$Elements = $HTMl.body.getElementsByTagName('tr')
$Headers = @()
foreach ($Element in $Elements) {
$ColumnID = 0
$HeaderRow = $false
$Object = New-Object -TypeName PSCustomObject
foreach ($Child in $Element.children) {
if ($Child.tagName -eq "th") {
$Headers += @($Child.outerText)
$HeaderRow = $true
}
if ($Child.tagName -eq "td") {
$Object | Add-Member -MemberType NoteProperty -Name $Headers[$ColumnID] -Value $Child.outerText
}
$ColumnID++
}
if (-not $HeaderRow) {
Write-Output -InputObject $Object
}
}
}
}