Adding a custom PowerCLI brush for syntax highlighting on WordPress

Print Friendly, PDF & Email

After moving my blog to a new hosting provider last month I was reviewing the WordPress plugins I use and I found myself wondering if Alex Gorbatchev’s SyntaxHighlighter supported PowerCLI. The WordPress plugin I use (courtesy of Alex Bond) had a Powershell plugin but no PowerCLI. Time to create! I’m by no means the first person to extend this plugin and I quickly realised there were two options;

  • upload a new ‘brush’ file to overwrite the existing Powershell brush. That change would be lost however if you upgraded the WordPress plugin and with the imminent release of Powershell v3 it could also be lost if the original Powershell brush was updated.
  • write an extra plugin which includes the new language. I felt this was a bit more work, but generally the better solution (plus I was half doing this to learn more about WordPress and the plugin structure). With clear guidance on how to create new languages the hard work was already done.

The result is my WordPress plugin for PowerCLI syntax highlighting which includes;


Add-EsxSoftwareDepot c:\tmp\VMware-Esxi-5.0.0-<buildnumber>
Remove-DeployRule -DeployRule FirstBoot -delete
Export-EsxImageProfile -ImageProfile "ESXiStatelessImage" -ExportToBundle -FilePath c:\tmp\


Add-EsxSoftwareDepot c:\tmp\VMware-Esxi-5.0.0-<buildnumber>
Remove-DeployRule -DeployRule FirstBoot -delete
Export-EsxImageProfile -ImageProfile "ESXiStatelessImage" -ExportToBundle -FilePath c:\tmp\

Not a massive difference agreed, but for me it’s akin to the VMware/VMWare argument – OCD rules my world! Code courtesy of Yellow Bricks.

Installing the plugin

NOTE: I’ve tested this under WP 3.1 and 3.3.

Simply follow the WordPress instructions which summarised are;

  1. Install the SyntaxHighlighter plugin (if you don’t already have it)
  2. Upload my plugin via WordPress Admin area (Admin -> Plugins -> Add New) or unzip and upload the files manually. This will extract two files into a new directory;
    • The PowerCLI brush which defines the syntax highlighting (shBrushPowerCLI.js)
    • The plugin itself (SyntaxHighlighterPowerCLI.php)
  3. Go to the WordPress Plugins menu and enable both SyntaxHighlighter and SyntaxHighligher-PowerCLI
  4.  Wrap any code you want highlighted in PCLI or POWERCLI tags.

Updating the plugin for new PowerCLI releases (or additional Powershell modules)

Once you’ve got the plugin working, updating the brush file (shBrushPowerCLI.js) to cater to new versions of PowerCLI (or alternative Powershell modules such as the Netapp Toolkit) is very easy. The most laborious part is formatting the text to include in the brush definition file so I figured I should script that too;

  1. Run the script below on a machine with a version of Powershell which is already running the cmdlets you want included (ie the VMware modules, Netapp’s ONTAP Toolkit etc).
  2. Simply cut and paste the output to the brush file after the ‘keywords = ‘Add-File …….’ clause.
  3. Upload the new brush file to the same directory as the SyntaxHighlighter plugin.


# choose ‘Cmdlet’ or ‘Alias’ to query with
$typetoUpdate = "Cmdlet"
#$typetoUpdate = "Alias"
$outputFile = "c:\output.txt"

$allcmds = Get-Command -Module vmware* -CommandType $typetoUpdate
$lastLine = $false

if ($typetoUpdate = "Cmdlet") {
$Output = "var keywords += "
$itemsPerLine = 5
} else {
$Output = "var alias += "
$itemsPerLine = 10

for($currentcmd = 0; $currentcmd -lt $allcmds.Length; $currentcmd += $itemsPerLine) {

# calculate the end index for this iteration
$endindex = $currentcmd + ($itemsPerLine – 1)
if ($endindex -ge $allcmds.Length) {
$endindex = $allcmds.Length – 1
$lastLine = $true

#insert a leading apostrophe at the start of each line
$Output += "’"
#insert each cmdlet Name
foreach($cmd in $allcmds[$currentcmd..$endindex]) {
$Output += $cmd.Name + " "
# add a traling apostrophe at the end of each line
$Output += "’"
# format line endings
if (!($lastLine)) {
$Output += " + "
} else {
$Output += ";"
$output += [Environment]::NewLine
$output | Out-File -FilePath $outputFile


Further Reading

2 thoughts on “Adding a custom PowerCLI brush for syntax highlighting on WordPress

  1. Nice. I’ve used SyntaxHighlighter in the past. Given that many PowerCLI scripts will include normal PowerShell commands though, and I don’t think that you can nest brushes with SyntaxHighlighter, you probably would want to remove “-Module vmware*” from line 06 and capture all commands from all modules wouldn’t you?
    I’ll try it out later and let you know.

  2. @Michael Poore
    Strewth, you’re eagle eyed!

    You’re right that many PowerCLI scripts will include normal PowerShell cmdlets, so in my brush file I’ve added a second ‘keywords += ‘‘ line so the PowerCLI cmdlets are in addition to the usual PowerShell commands, not instead of. I somehow missed that in the script which generates the ‘keywords +=’ line however. It’s duly updated.

    It’s also worth pointing out that if you want to add cmdlets from other vendors (Netapp’s ONTAP Toolkit for example) you’ll need to amend line 6. Either drop the ‘-Module vmware*’ part (as Michael rightly points out) or filter by your vendor of choice. Thanks!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.