How to display formatted PowerShell scripts in WordPress post

Posting source code has never been easier in WordPress!

I have spent the better portion of this past week writing specialized scripts to transform my existing scripts into “formatted HTML” so I can include source in my blog posts. Needless to say, it has been WAY too much administrative effort for such a simple task. I was literally searching and finding every space in my scripts and replacing them with &nbsp, or having to search my code and enclose every special character with an HTML FONT tag… This of course before having to manually set different colors for all my variables, strings, cmdlets, attributes..etc…

The reason behind that madness, or problem I was running into mostly, was that my source code (aside from looking terrible in all black font without line numbers) was being “word-wrapped” automatically and the structure and organization would be lost as soon as I posted anything. This would make it very difficult for a viewer to easily copy and paste into a shell window and execute the script without editing it first.

With the help of a fellow blogger Braden Lenz (http://bradenlenz.com) who found a great post (Source Link) I was able to achieve code posting bliss.

In the example below, I wrote up a quick PowerShell script to send an HTML formatted email using built in cmdlets. Feel free to save this function for use in your own scripts.

Before:

I created an HTML div tag that was horizontally scrollable, and mimicked the PowerShell console. (blue background, white text)

function SendEmail {
# Configuration of Email Communication (SMTP, Sender, Receipent)
$smtpServer = ‘mail.redwings.com’
$MailFrom = ‘jimmy.howard@redwings.com’
$mailto = ‘pavel.datsyuk@redwings.com’# Create email object
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)# Content of email
$file = “C:2013-LineupWingsVSHawks.csv”
$att = New-Object Net.Mail.Attachment($file)
$msg.Attachments.Add($att)
$msg.From = $MailFrom
$msg.IsBodyHTML = $true
$msg.To.Add($Mailto)
$msg.Subject = “Red Wings Linup for: $(get-date -f MM-dd-yyyy)”
$MailTextT = Get-Content -Path C:2013-LineupWingsVSHawks.htm
$msg.Body = $MailTextT# Call to send the email
$smtp.Send($msg)
}

After:

I simply copy and pasted my script right into my post and enclosed it in a sourcecode tag.

The Result:

[sourcecode language=”powershell” wraplines=”false” collapse=”false”]

function SendEmail {

# Configuration of Email Communication (SMTP, Sender, Receipent)

$smtpServer = ‘mail.redwings.com’

$MailFrom = ‘jimmy.howard@redwings.com’

$mailto = ‘pavel.datsyuk@redwings.com’

# Create email object

$msg = new-object Net.Mail.MailMessage

$smtp = new-object Net.Mail.SmtpClient($smtpServer)

# Content of email

$file = "C:2013-LineupWingsVSHawks.csv"

$att = New-Object Net.Mail.Attachment($file)

$msg.Attachments.Add($att)

$msg.From = $MailFrom

$msg.IsBodyHTML = $true

$msg.To.Add($Mailto)

$msg.Subject = "Red Wings Linup for: $(get-date -f MM-dd-yyyy)"

$MailTextT = Get-Content -Path C:2013-LineupWingsVSHawks.htm

$msg.Body = $MailTextT

# Call to send the email

$smtp.Send($msg)

}

[/sourcecode]

To summarize, WordPress recognizes the SOURCECODE tag to allow for automatic formatting of many languages like PowerShell, JavaScript, Python..etc. It also can be configured to allow for expanding code boxes, word wrap, and more. It’s just too bad this information was so hard to come by, you think the WordPress team might think about adding it as a built-in option in their post editor.

-Bryan

Tags// ,