Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active October 1, 2022 14:52
Show Gist options
  • Save Jaykul/43f7655a6d7be83857c56df3ef9bd96e to your computer and use it in GitHub Desktop.
Save Jaykul/43f7655a6d7be83857c56df3ef9bd96e to your computer and use it in GitHub Desktop.
Grasping at Formatting Straws
function Out-HtmlTable {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
$InputObject
)
begin {
$Buffer = [System.Collections.ArrayList]::new()
if($InputObject) {
$Buffer.Add($InputObject)
}
}
process {
$null = $Buffer.Add($InputObject)
}
end {
$html = $(
foreach($item in $Buffer | Format-Table) {
switch -regex ($item.PSTypeNames) {
"GroupStartData" {
$GroupName, $GroupValue = $item.groupingEntry.formatValueList.formatValueList.formatValueList.formatValueList | % {
if($_.text) { $_.text}
if($_.propertyValue){ $_.propertyValue }
}
}
"FormatStartData" {
$labels = $item.shapeInfo.tableColumnInfoList.label
}
"FormatEntryData" {
$values = $item.formatEntryInfo.formatPropertyFieldList.propertyValue
$ht = @{}
# $ht.PSTypeName = $Buffer[$xx].PSTypeNames[0]
for($i = 0; $i -lt $labels.Count; $i++) {
$ht.($labels[$i]) = $values[$i]
}
# write-verbose -verbose ($pv.PSTypeNames -join "|")
[PSCustomObject]$ht
}
}
}
) | Microsoft.PowerShell.Utility\ConvertTo-Html -As Table -Fragment -Property $labels
$html = $html -replace "^<table>","<table class='table table-striped'>"
"<link href='https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css' rel='stylesheet' />" + $html + @'
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function(){
$('table.table').DataTable();
});
</script>
'@
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment