Modify PowerGUI snippets to function in the PowerShell ISE


Recently I discovered Glen Scales post “EWS Snippets for PowerGui” Although PowerGUI now works with version 3 of PowerShell, I am still using the PowerShell ISE for script development.

So, I wrote a script to convert PowerGUI snippets to PowerShell ISE snippets. Both PowerGUI and PowerShell ISE snippets are written in XML. Sadly, I am not much into XML, so I Bing’d “PowerShell Create XML” and found Creating XML Documents from PowerShell. That got me started on my script, and most importantly, led me to the MSDN documentation for the XmlWriter Methods.

I have tested the script on the EWS Snippets for PowerGui and the JAMS Job Scheduler snippets and it seems to work well.

All of the parameters are optional. If you do not specify the optional “SnippetPath” parameter, the script defaults to using the snippet path of <User>\<MyDocuments>\WindowsPowerShell\snippets.

The script accepts the optional switch parameter “KeepPowerGUI” which, if set, will keep the PowerGUI version of the snippets. Another optional switch parameter, “Append” will append the specified “SnippetPath” to the snippet path of <User>\<MyDocuments>\WindowsPowerShell\snippets.

The script is published on http://gallery.technet.microsoft.com/Convert-PowerGUI-Snippets-d05c17b3 or you can copy it from below.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<#
.SYNOPSIS
    ConvertFrom-PowerGUI.ps1 – Modifies PowerGUI Snippets to work with the PowerShell ISE.
.DESCRIPTION
    This script modifies PowerGUI Snippets to work with the PowerShell ISE.
.EXAMPLE
    .\ConvertFrom-PowerGUI.ps1
    Description
    Modifies existing PowerGUI snippets to work with the PowerShell ISE.
.EXAMPLE
    .\ConvertFrom-PowerGUI.ps1 -KeepPowerGUI
    Description
    Modifies existing PowerGUI snippets to work with the PowerShell ISE, while preserving the PowerGUI versions.
.EXAMPLE
    .\ConvertFrom-PowerGUI.ps1 -SnippetPath C:\Scripts\Snippets
    Description
    Modifies existing PowerGUI snippets in the path C:\Scripts\Snippets to work with the PowerShell ISE.
.EXAMPLE
    .\ConvertFrom-PowerGUI.ps1 -SnippetPath “TestSnippets” -Append
    Description
    Modifies existing PowerGUI snippets in the path <User>\<MyDocuments>\WindowsPowerShell\snippets\TestSnippets to work with the PowerShell ISE.
.NOTES
    Created: 05/17/2013 Karl Mitschke
.PARAMATER KeepPowerGUI
    When set, keeps the PowerGUI snippets.
.PARAMATER SnippetPath
    If specified, converts snippets in the SnippetPath.
    If not specified, will use the default snippet path of <User>\<MyDocuments>\WindowsPowerShell\snippets.
.PARAMATER Append
    If specified, appends the specified SnippetPath to the default snippet path of <User>\<MyDocuments>\WindowsPowerShell\snippets.
#>

[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact=‘Medium’)]
param (
[Parameter(
Mandatory = $false
)]
[switch]$KeepPowerGUI,
[Parameter(
Mandatory = $false
)]
[string]$SnippetPath,
[Parameter(
Mandatory = $false
)]
[switch]$Append
)

begin{
    [bool]$PathExists = $false
    $DefaultSnippetPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments)
    $DefaultSnippetPath += “\WindowsPowerShell\Snippets\”
    if ($SnippetPath){
        $SnippetDirectory = $SnippetPath
    }
    else{
        $SnippetDirectory = $DefaultSnippetPath
    }
    if ($Append){
        $SnippetDirectory = $DefaultSnippetPath
        $SnippetDirectory += $SnippetPath |Split-Path -Leaf
    }
    $PathExists = Test-Path $SnippetDirectory
    if (!($PathExists)){
        Write-Warning “The path ‘$($SnippetDirectory)’ does not seem to exist.”
        if ($SnippetDirectory -notlike “$DefaultSnippetPath*”){
            Write-Warning “If you want to append the path ‘$($SnippetPath)’ to the default snippet path, use the -Append parameter.”
        }
        break
    }
}
process{
    if ($pscmdlet.ShouldProcess(“$($SnippetDirectory)”,“Convert PowerGUI snippets”)){
        Get-ChildItem -Path $SnippetDirectory -Include *.snippet -Recurse |ForEach-Object{
            [xml]$File = Get-Content $_
            $OutFile = $_.FullName.ToString()
            $OutFile += “s.ps1xml”
            $XmlWriter = New-Object System.XMl.XmlTextWriter($OutFile,[System.Text.Encoding]::UTF8)
            $xmlWriter.Formatting = “Indented”
            $xmlWriter.Indentation = “4”
            $xmlWriter.WriteStartDocument()
            $xmlWriter.WriteStartElement(“Snippets”)
            $xmlWriter.WriteAttributeString(“xmlns”,http://schemas.microsoft.com/PowerShell/Snippets&#8217;)
            $xmlWriter.WriteStartElement(“Snippet”)
            $XmlWriter.WriteAttributeString(“Version”,“1.0.0”)
            $xmlWriter.WriteStartElement(“Header”)
            $xmlWriter.WriteStartElement(“Title”)
            $xmlWriter.WriteValue($file.CodeSnippets.CodeSnippet.Header.Title)
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteStartElement(“Description”)
            $xmlWriter.WriteValue($file.CodeSnippets.CodeSnippet.Header.Description)
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteStartElement(“Author”)
            $xmlWriter.WriteValue($file.CodeSnippets.CodeSnippet.Header.Author)
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteStartElement(“ModifiedForISE”)
            $xmlWriter.WriteValue(“Karl Mitschke”)
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteStartElement(“SnippetTypes”)
            $xmlWriter.WriteStartElement(“SnippetType”)
            $xmlWriter.WriteValue(“Expansion”)
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteStartElement(“Code”)
            $xmlWriter.WriteStartElement(“Script”)
            $xmlWriter.WriteAttributeString(“Language”,“PowerShell”)
            $xmlWriter.WriteAttributeString(“CaretOffset”,“13”)
            $xmlWriter.WriteValue($file.CodeSnippets.CodeSnippet.Snippet.Code.ChildNodes.Data)
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteEndElement()
            $xmlWriter.WriteEndDocument()
            $xmlWriter.Finalize
            $xmlWriter.Flush()
            $xmlWriter.Close()
        }
    }
}
end{
    if ($host.Name -eq “Windows PowerShell ISE Host”){
        Import-IseSnippet -Path $SnippetDirectory -Recurse
    }
    if (!($KeepPowerGUI)){
        Get-ChildItem -Path $SnippetDirectory -Include *.snippet -Recurse |Remove-Item -Force |Out-Null
    }
}

Advertisement
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: