Wednesday, December 17, 2008

Online Syntax Highlighting for Powershell

If you are posting PowerShell code sample and want to format the text nicely, there’s an on-line high-lighter tool that will do this for you available here.

This formatting tool is pretty nice. Consider the following script (from http://pshscripts.blogspot.com):

# Parse and TryParse static methods on Int64 type # Sample using PowerShell # Thomas Lee - tfl@psp.co.uk

# Create an int64 and two strings $i=new-object system.int64 "varable `$i created - type is {0}" -f $i.GetTypeCode() $s1="1234" $s2="12345678934212321212"

# Use the Parse static method on S1 that WILL work OK $i=[system.int64]::parse($s1) "The integer `$i={0} - parsed OK as Int64" -f $s1 ""

# Use the TryParse static method with small and too large a number $result=[system.int64]::tryparse($s1,[ref] $i) "The string `"{0}`" parsed correctly as an int64: {1}" -f $s1,$result $result=[system.int64]::tryparse($s2,[ref] $i) "The string `"{0}`" parsed correctly as an int64: {1}" -f $s2,$result

Using the online Highligher tool creates HTML you can cut/past into your web site, thus you end up with something that looks like this:

  1. # Parse and TryParse static methods on Int64 type
  2. # Sample using PowerShell
  3. # Thomas Lee - tfl@psp.co.uk
  4. # Create an int64 and two strings
  5. $i=new-object system.int64
  6. "Variable `$i created - type is {0}" -f $i.GetTypeCode()
  7. $s1="1234"
  8. $s2="12345678934212321212"
  9. # Use the Parse static method on S1 that WILL work OK
  10. $i=[system.int64]::parse($s1)
  11. "The integer `$i={0} - parsed OK as Int64" -f $s1
  12. ""
  13. # Use the TryParse static method with small and too large a number
  14. $result=[system.int64]::tryparse($s1,[ref] $i)
  15. "The string `"{0}`" parsed correctly as an int64: {1}" -f $s1,$result
  16. $result=[system.int64]::tryparse($s2,[ref] $i)
  17. "The string `"{0}`" parsed correctly as an int64: {1}" -f $s2,$result

The Highlighter tool can create the HTML fragement with or without a link to a CSS style sheet. In using this tool with Live Writer to produce this blog posting there seems to be little difference. To demonstrate this point, here’s the code without a CSS link:

  1. # Parse and TryParse static methods on Int64 type
  2. # Sample using PowerShell
  3. # Thomas Lee - tfl@psp.co.uk
  4. # Create an int64 and two strings
  5. $i=new-object system.int64
  6. "varable `$i created - type is {0}" -f $i.GetTypeCode()
  7. $s1="1234"
  8. $s2="12345678934212321212"
  9. # Use the Parse static method on S1 that WILL work OK
  10. $i=[system.int64]::parse($s1)
  11. "The integer `$i={0} - parsed OK as Int64" -f $s1
  12. ""
  13. # Use the TryParse static method with small and too large a number
  14. $result=[system.int64]::tryparse($s1,[ref] $i)
  15. "The string `"{0}`" parsed correctly as an int64: {1}" -f $s1,$result
  16. $result=[system.int64]::tryparse($s2,[ref] $i)
  17. "The string `"{0}`" parsed correctly as an int64: {1}" -f $s2,$result
Technorati Tags: ,

No comments: