Skip to content

Instantly share code, notes, and snippets.

@russellds
Created March 21, 2014 15:15
Show Gist options
  • Save russellds/9688526 to your computer and use it in GitHub Desktop.
Save russellds/9688526 to your computer and use it in GitHub Desktop.
ConvertFrom-Html
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