A while back I switched to GeSHi for source code highlighting in my posts, and recently I started writing PowerShell scripts in my posts, and calmly sticking it in <code lang=“posh”> tags, half expecting it to just work, like all the other languages … but of course it didn’t. So after a few searches on Google and Ask, I concluded that a PowerShell syntax file doesn’t yet exist. So, I made one, feel free to grab it (it’s GPL, and I’ve even left intact the “any later version” clause from GeSHi’s license).

EDIT: 6/10/2007
I should mention that the way I do highlighting doesn’t care about “Nouns” at all: it just uses a list of Verbs, and matches anything that starts with a “Verb-” ... that means that for the sake of the highlighting, you can’t just say “Content” for Get-Content or (even though that actually works in a script). Also, I changed the version I was using so that it uses regular expression patters for command parameters, instead of a list. Because you can abbreviate parameters to the shortest distinguishable form, a list doesn’t really work.

Let’s just see in action, shall we?

# Get-Sql.ps1
###################################################################################################
# NOTICE that I have set default values for the server and database, but not for the query
# ALSO NOTICE that I'm not handling authentication (I just use Integrated Security)
#
#   By default when you run this script it:
#     * creates the functions
#     * initializes the connection
#   But we don't automatically do a query -- unless you pass one in!
#
#   Thus, calling the script with no parameters results in an initialized connection,
#   but it doesn't return anything, so it's basically silent if there are no errors.
#

# the default server and database
param( $Server = ".", $Database = "SOMR",  $Query )

#
# change the SqlConnection (it's set to a default when the script it run)
#
function global:Set-SqlConnection( $Server = $(Read-Host "SQL Server Name"), $Database = $(Read-Host "Default Database") ) {
  $SqlConnection.ConnectionString = "Server = $Server; Database = $Database; Integrated Security = True"
}

#
# A function to allow the user to
# perform a query that returns a table full of data
#
function global:Get-SqlDataTable( $Query = $(Read-Host "Enter SQL Query")) {

  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = $Query
  $SqlCmd.Connection = $SqlConnection

  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $SqlCmd

  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet)

  $SqlConnection.Close()
 
  return $DataSet.Tables[0]
}

# Initialize the SqlConnection variable
Set-Variable SqlConnection (New-Object System.Data.SqlClient.SqlConnection) -Scope Global -Option AllScope -Desc "Personal variable for Sql Query functions"

# Initially create the SqlConnection
Set-SqlConnection $Server $Database

# go ahead and run the initial query if we have one...
if( $query -gt $null ) {
  Get-SqlDataTable $Query
}

# Some aliases to let you use the functions with less typing
Set-Alias gsdt Get-SqlDataTable -Option AllScope -Description "Personal Function alias from Get-Sql.ps1"
Set-Alias ssc Set-SqlConnection -Option AllScope -Description "Personal Function alias from Get-Sql.ps1"
 

Now if only I could get it working that well in SciTE!

2 Responses to “PowerShell Highlighting for GeSHi”

  • Dalle says:

    Hello,

    Thank you!

    I thought I needed to write one of my own.
    This is great!

    I have been useing for only 1 day now but I have a problem with >.
    When I write It i get the html code for It insted. Do you have the same problem?

    Regards,
    Fredrik Wall

  • Uhm … geshi messes with stuff it considers unsafe in HTML, so if you try to put in > (a greater-than sign) geshi will turn it into &gt; ... let me explain by writing the same thing in plain HTML and piped into geshi:

    get-pasting &gt; outfile.txt
    get-pasting &amp;gt; out2.txt

    and out:

    get-pasting > outfile.txt
    get-content outfile.txt &gt; out2.txt