Skip to content

Instantly share code, notes, and snippets.

@altrive
Created August 14, 2013 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save altrive/6227323 to your computer and use it in GitHub Desktop.
Save altrive/6227323 to your computer and use it in GitHub Desktop.
Cmdlet to show object tree in HtmlView

Summary

This Cmdlet show object tree HtmlView by using LINQPad HTML Writer.

Prerequisite

This Cmdlet use LINQPad.exe http://www.linqpad.net/

Usage

Get-WinEvent Application -MaxEvents 10  | Out-HtmlView
Get-Command  Get-Help | Out-HtmlView -Depth 10
#TODO:support for non serializable object(example:PSCustomObject)
function Out-HtmlView
{
[CmdletBinding()]
param(
[Parameter()]
[int]$Depth = 2,
[Parameter(Mandatory,ValueFromPipeLine)]
$InputObject
)
begin
{
$ErrorActionPreference = "Stop"
#Load LinqPad assembly
$exePath = Join-Path ${env:ProgramFiles(x86)} "LINQPad4\LINQPad.exe" -Resolve
[Reflection.Assembly]::LoadFile($exePath) > $null
#Load WPF assembly for Powershell.exe
Add-Type -AssemblyName PresentationFramework
#Create LINQPad HTML Writer
$writer = [LINQPad.Util]::CreateXhtmlWriter($false)
#Create WPF Window object
$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<WebBrowser x:Name="Browser"/>
</Grid>
</Window>
"@
$window = [Windows.Markup.XamlReader]::Parse($xaml)
$window.Topmost = $True
$browser = $Window.FindName("Browser")
}
process
{
#Need to use foreach to support -InputObject parameter
foreach($item in $InputObject)
{
#if(($item -is [PSObject]) -and ($item.PSObject.BaseObject -ne $null))
#{
# $item = $item.PSObject.BaseObject #Unwrap object
#}
$writer.WriteDepth($item,$Depth)
}
}
end
{
try{
#Write-Host ("FormattingTime: {0} [ms]" -f $writer.FormattingTime.TotalMilliSeconds)
$html = $writer.ToString()
$writer.Dispose()
$browser.NavigateToString($html)
#Use Dispatcher.InvokeAsync to avoid problem<https://gist.github.com/altrive/6227237>
$async = $window.Dispatcher.InvokeAsync({
$window.ShowDialog() | Out-Null
})
$async.Wait() | Out-Null
}
catch{
Write-Error $_.Exception
}
finally{
if($browser -ne $null){
$browser.Dispose()
}
if($window -ne $null){
$window.Close()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment