PowerShell : Generating RTF Help from XML help files
In a post on my former blog, /\/\o\/\/ PowerShelled: Read the raw Monad XML helpfiles , I showed how to parse the XML help file of PowerShell
(as this was in a Beta version of PowerShell you need to change the extension $mshHome by $PSHOME)
# XmlHelp.PS!
# Reading Xml Helpfile Example
# /\/\o\/\/ 2006
ls -recurse $PSHOME *-Help.xml | foreach {
write-host -fore "blue" $_.Name
$Help = [xml](gc $_.fullName)
$help.helpitems.command | foreach {
write-host -fore "red" $_.details.name;$_.details.description.get_InnerText()
$_.Examples.example | foreach {write-host -fore "White"$_.title.para;write-host -fore "White"$_.code;$_.results}
}
}
that did rip out all examples showing them a bit more collorfull like this :
I go on from that here using a Rich Text box to output the help in RTF format, and to show it in a form,
that looks like this :
If you past the contents of this box into word you have a 164 Pages PowerShell Command Reference,
From here you can adapt the script so that the exported documentation is formatted the way that YOU like, for example add the systax setting or identing font extc.
And here the script :
(*Edit* as I use the Forms library you need to have that loaded : [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") Thanks for the comment Jim V, as I load it in my profile I keep forgettting that)
# Get-RtbHelp.ps1
#
# Generates a Rich Text PowerShell Command help from the XML help files
# and Shows this in a Form
#
# /\/\o\/\/ 2006
# http://ThePowerShellGuy.com
Function get-RtbHelp {
$FontBold = new-object System.Drawing.Font("Microsoft Sans Serif",10,[Drawing.FontStyle]'Bold' )
$FontHeader = new-object System.Drawing.Font("Microsoft Sans Serif",14,[Drawing.FontStyle]'Bold' )
$fontNormal = new-object System.Drawing.Font("Microsoft Sans Serif",10,[Drawing.FontStyle]'Regular')
$fontItalic = new-object System.Drawing.Font("Microsoft Sans Serif",10,[Drawing.FontStyle]'Italic')
$fontCode = new-object System.Drawing.Font("Lucida Console",8,[Drawing.FontStyle]'Bold' )
$form = new-object Windows.Forms.form
$form.Size = new-object System.Drawing.Size @(1000,800)
$form.text = "PowerShell Command overview"
$RTB = new-object System.Windows.Forms.RichTextBox
$RTB.Dock = [System.Windows.Forms.DockStyle]::Fill
$RTB.Font = $fontNormal
$RTB.DetectUrls = $false
$form.Controls.Add($RTB)
$rtb.SelectionColor = 'Green'
$RTB.selectionFont = $fontHeader
$rtb.appendText("PowerShell Command overview`n")
$RTB.selectionFont = $fontBold
$RTB.appendText("Generated from XML Help files by Get-RtbHelp`n")
$RTB.appendText("/\/\o\/\/ 2006 `nhttp://ThePowerShellGuy.com`n")
ls -recurse $PSHOME *-Help.xml | foreach {
$rtb.SelectionColor = 'Green'
$RTB.selectionFont = $fontHeader
$RTB.appendText("`n`nHelp File : $($_.Name)`n")
$rtb.SelectionColor = 'Black'
$Help = [xml](get-content $_.fullName)
$help.helpitems.command |
foreach {
$RTB.selectionFont = $fontHeader
$rtb.SelectionColor = 'Blue'
$RTB.appendText("____________________ $($_.details.name)")
$rtb.SelectionIndent = 10
$rtb.SelectionColor = 'Black'
$RTB.appendText("`n")
$RTB.selectionFont = $fontBold
$RTB.appendText($_.details.description.get_InnerText())
$RTB.appendText("`n`n")
$RTB.selectionFont = $fontItalic
$RTB.appendText($_.description.get_innertext())
$RTB.appendText("`n")
$RTB.selectionFont = $fontBold
$RTB.appendText("`nParameters:`n`n")
$_.syntax.syntaxItem |foreach {
$_.parameter | foreach {
$RTB.selectionFont = $fontBold
$rtb.SelectionIndent = 20
$RTB.appendText("-$($_.name)")
$RTB.appendText("`n")
$RTB.selectionFont = $fontNormal
$rtb.SelectionIndent = 40
$RTB.appendText($_.description.para)
$RTB.appendText("`n")
$rtb.SelectionIndent = 10
}
}
$RTB.selectionFont = $fontBold
$RTB.appendText("`nExamples:`n`n")
$_.Examples.example | foreach {
$rtb.SelectionIndent = 40
$RTB.selectionFont = $fontCode
$rtb.SelectionColor = 'Red'
$RTB.appendText($_.introduction.get_innertext())
$rtb.SelectionColor = 'Black'
$RTB.appendText($_.code)
$RTB.appendText("`n")
$RTB.selectionFont = $fontCode
$RTB.appendText($_.results)
$RTB.appendText("`n")
$rtb.SelectionIndent = 60
$RTB.selectionFont = $fontNormal
$RTB.appendText($_.Remarks.get_innertext())
$RTB.appendText("`n`n")
$rtb.SelectionIndent = 0
}
}
}
$Form.Add_Shown({$form.Activate()})
[void]$form.showdialog()
}
get-RtbHelp
Enjoy,
Greetings,/\/\o\/\/
Tags : PowerShell