Created
March 21, 2014 15:15
-
-
Save russellds/9688526 to your computer and use it in GitHub Desktop.
ConvertFrom-Html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertFrom-Html { | |
#.Synopsis | |
# Convert a table from an HTML document to a PSObject | |
#.Example | |
# Get-ChildItem | Where { !$_.PSIsContainer } | ConvertTo-Html | ConvertFrom-Html -TypeName Deserialized.System.IO.FileInfo | |
# Demonstrates round-triping files through HTML | |
param( | |
# The HTML content | |
[Parameter(ValueFromPipeline=$true)] | |
[string]$Html, | |
# A TypeName to inject to PSTypeNames | |
[string]$TypeName | |
) | |
begin { $content = "$html" } | |
process { $content += "$html" } | |
end { | |
[xml]$table = $content -replace '(?s).*<table[^>]*>(.*)</table>.*','<table>$1</table>' | |
$header = $table.table.tr[0] | |
$data = $table.table.tr[1..$table.table.tr.Count] | |
foreach($row in $data){ | |
$item = @{} | |
$h = "th" | |
if(!$header.th) { | |
$h = "td" | |
} | |
for($i=0; $i -lt $header.($h).Count; $i++){ | |
if($header.($h)[$i] -is [string]) { | |
$item.($header.($h)[$i]) = $row.td[$i] | |
} else { | |
$item.($header.($h)[$i].InnerText) = $row.td[$i] | |
} | |
} | |
Write-Verbose ($item | Out-String) | |
$object = New-Object PSCustomObject -Property $item | |
if($TypeName) { | |
$Object.PSTypeNames.Insert(0,$TypeName) | |
} | |
Write-Output $Object | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment