In my last post, I showed you a teaser of an upcoming post. So, this post is about the PowerShell ISE and PowerGUI 2.1 Script Editor (PGSE) addon for uploading code to PasteBin.com. This happens to be my first WPF addon for PowerShell ISE. The PGSE version of this will soon be out. I am looking at working around a bug in PowerShell. Looks pretty hard right now.
Now, coming to the subject, this addon module is written in such as way that it detects what scripting environment you are using and then extends the script editor menu options to add “Send to PasteBin” item. When you select some text in the editor and click “Send to PasteBin” or CTRL+B, the following WPF window appears.
You will see the above interface in PowerShell ISE. The top text box will have the content you selected in the script editor. The remaining options are quite self-explanatory. You can read about these options at http://pastebin.com/api.php.
You can use the “save preferences” button to make this addon remember you email ID, subdomain name, script synatx, expiry date, and visibility settings between sessions. These settings get stored in a XML file under $env:APPDATA. I could have used the .NET isolated storage here but there seems to be an issue with XML serializer & PowerShell custom objects. So, I will hopefully fix this and the PGSE 2.1 issue in the next release.
Once you click the “Submit to PasteBin” button and if the upload is successful, you will see a web browser window loading the URL to the newly uploaded content. So, without futher delay, you can download the code for this addon module here.
Ipmo -global WPK -Force Ipmo -global PasteBin -Force Function Get-PasteBinPreferences { if (Get-Item $global:xmlPath -ErrorAction SilentlyContinue) { try { $pbPreferences = Import-Clixml -Path $global:xmlPath $global:PBSyntax = $pbPreferences.Syntax $global:PBExpiry = $pbPreferences.Expiry $global:PBExposure = $pbPreferences.Exposure $global:PBSubDomain = $pbPreferences.SubDomain $global:PBEmail = $pbPreferences.Email } catch { Write-Host "Import-CliXml failed with following error" return } } else { $global:PBSyntax = "PowerShell" $global:PBExpiry = "Never" $global:PBExposure = "Public" $global:PBSubDomain = "" $global:PBEmail = "" } } Function Save-PasteBinPreferences { param($syntax, $expiry, $exposure, $subdomain, $email) $pbPreferences = New-Object PSObject $pbPreferences | Add-Member -MemberType NoteProperty -Name "Syntax" -Value $syntax $pbPreferences | Add-Member -MemberType NoteProperty -Name "Expiry" -Value $expiry $pbPreferences | Add-Member -MemberType NoteProperty -Name "Exposure" -Value $exposure $pbPreferences | Add-Member -MemberType NoteProperty -Name "Subdomain" -Value $subdomain $pbPreferences | Add-Member -MemberType NoteProperty -Name "Email" -Value $email try { Export-Clixml -InputObject $pbPreferences -Path $global:xmlPath -Force } catch { Write-Host "Export-Clixml; failed with the following error" Write-Host $error[0].InnerException return } [system.Windows.Forms.MessageBox]::show('Your preferences have been saved') } function Show-PasteBinUI { if (($global:SelectedText -eq "") -or ($global:SelectedText -eq $null)) { [system.Windows.Forms.MessageBox]::show('You must select some text to continue') return } Get-PasteBinPreferences New-Window -Title "PasteBin addon" -WindowStartupLocation CenterScreen -Width 836 -Height 477 -Show -ResizeMode NoResize -On_Loaded { $txtPasteCode = $Window | Get-ChildControl txtPasteCode $cmbSyntax = $Window | Get-ChildControl cmbSyntax $cmbExpiry = $Window | Get-ChildControl cmbExpiry $cmbExposure = $Window | Get-ChildControl cmbExposure $txtTitle = $window | Get-ChildControl txtTitle $txtSubDomain = $window | Get-ChildControl txtSubDomain $txtEmail = $window | Get-ChildControl txtEmail $btnSavePref = $window | Get-ChildControl btnSavePref $btnSubmit = $window | Get-ChildControl btnSubmit } { New-Grid { New-TextBox -Name txtPasteCode -Margin "12,12,0,0" -Height 258 -Width 800 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Text $global:SelectedText ` -IsReadOnly -VerticalScrollBarVisibility "Auto" -HorizontalScrollBarVisibility "Auto" New-Label -Name lblLang -Margin "12,281,0,0" -Height 28 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Scripting Language" -FontWeight "Bold" New-ComboBox -Name cmbSyntax -Margin "175,285,0,0" -Height 23 -Width 192 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Items $global:cmbSyntaxOptions -SelectedIndex $global:cmbSyntaxOptions.IndexOf($global:PBSyntax) New-Label -Name lblExpiry -Margin "12,321,0,0" -Height 28 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Expires in" -FontWeight "Bold" New-ComboBox -Name cmbExpiry -Margin "175,324,0,0" -Height 23 -Width 192 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Items $global:cmbExpiryOptions -SelectedIndex $global:cmbExpiryOptions.IndexOf($global:PBExpiry) New-Label -Name lblExposure -Margin "12,361,0,0" -Height 28 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Visibility" -FontWeight "Bold" New-ComboBox -Name cmbExposure -Margin "175,361,0,0" -Height 23 -Width 192 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Items $global:cmbExposureOptions -SelectedIndex $global:cmbExposureOptions.IndexOf($global:PBExposure) New-Label -Name lblTitle -Margin "450,281,0,0" -Height 28 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Title" -FontWeight "Bold" New-TextBox -Name txtTitle -Margin "570,285,0,0" -Height 23 -Width 192 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" New-Label -Name lblSubDomain -Margin "450,321,0,0" -Height 28 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Sub Domain" -FontWeight "Bold" New-TextBox -Name txtSubDomain -Margin "570,324,0,0" -Height 23 -Width 192 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Text $global:PBSubDomain New-Label -Name lblEmail -Margin "450,361,0,0" -Height 28 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Email Address" -FontWeight "Bold" New-TextBox -Name txtEmail -Margin "570,361,0,0" -Height 23 -Width 192 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Text $global:PBEmail New-Button -Name btnSavePref -Margin "280,400,0,0" -Height 23 -Width 110 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Save Preferences" -On_Click { Save-PasteBinPreferences -Syntax $cmbSyntax.SelectedItem -Expiry $cmbExpiry.SelectedItem -Exposure $cmbExposure.SelectedItem -SubDomain $txtSubDomain.Text -Email $txtEmail.Text } New-Button -Name btnSubmit -Margin "410,400,0,0" -Height 23 -Width 110 ` -HorizontalAlignment "Left" -VerticalAlignment "Top" -Content "Submit to PasteBin" -On_Click { if ($global:cmbExposureOptions.IndexOf($cmbExposure.SelectedItem) -eq 1) { $returnUri = New-PasteBin -pasteCode $global:SelectedText -pasteName $txtTitle.Text ` -pasteEmail $txtEmail.Text -pasteSubDomain $txtsubDomain.Text ` -pasteExpireDate $ExpiryHash[$cmbExpiry.SelectedItem] -pasteFormat $cmbSyntax.SelectedItem -pastePrivate } else { $returnUri = New-PasteBin -pasteCode $global:SelectedText -pasteName $txtTitle.Text ` -pasteEmail $txtEmail.Text -pasteSubDomain $txtsubDomain.Text ` -pasteExpireDate $ExpiryHash[$cmbExpiry.SelectedItem] -pasteFormat $cmbSyntax.SelectedItem } if ($returnUri) { [System.Diagnostics.Process]::Start($returnUri) $window.Close() } else { [system.Windows.Forms.MessageBox]::show('Error occured while uploading to PasteBin') $window.Close() } } } } } [System.Collections.ArrayList]$global:cmbSyntaxOptions = "PowerShell","VB","cpp","C","Java","mysql","pascal","oracle11","oracle8","python","perl","sql","tsql","vbnet","xml","csharp" $global:ExpiryHash = @{"Never"="N";"Ten Minutes"="10M";"One Hour"="1H";"One Day"="1D";"One Month"="1M"} [System.Collections.ArrayList]$global:cmbExpiryOptions = $global:ExpiryHash.Keys [System.Collections.ArrayList]$global:cmbExposureOptions = "Public","Private" $global:xmlPath = $("$env:APPDATA\PasteBinPrefs.xml") if ($host.Name –eq 'Windows PowerShell ISE Host') { $scriptBlock = { $global:SelectedText = $psISE.CurrentFile.Editor.SelectedText Show-PasteBinUI } $submenus = $psise.CurrentPowerShellTab.AddOnsMenu.Submenus $menuExists = $false foreach ($menuItem in $subMenus) { if ($menuItem.DisplayName -eq "Send to Paste _Bin") { $menuExists = $true } } if (!$menuExists) { $psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("Send To Paste _Bin",$ScriptBlock,"CTRL+B") } $ExecutionContext.SessionState.Module.OnRemove = { foreach ($menuItem in $subMenus) { if ($menuItem.DisplayName -eq "Send to Paste _Bin") { $submenus.Remove($menuItem) return } } } } else { Write-Host "This module is meant for either ISE or PGSE" return } Export-ModuleMember -Function * -Variable *
Do try this out and let me know how you find it.


{ 3 comments… read them below or add one }
Hello
Thank for post its really useful article to me.
Hi RAVIKANTHCHAGANTI,
I’m about your complaint regarging events in a WPK application. A time ago I investigated into the problem you raised. I’m not subscriber of the private beta forum so that I passed a sample on how to use events through another member of the community. Didn’t you receive the sample?
Here is the same, both as a code excerpt and a short article:
http://powershelldevtools.wordpress.com/2010/06/21/how-to-deal-with-routedevent-parameter-in-wpk/
You’d planned to be the first 3rd party add-on developer who posted an add-on, the first place is still free and the community welcomes contributors.
Hi Alexander,
I have not got this sample. I will try this soon and sorry about the delayed reply. I have been quite busy at work and unable to check this blog on a regular basis.
{ 1 trackback }