PoshCode Logo PowerShell Code Repository

Send-Growl 3.1 (modification of post by Joel Bennett view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1464"></script>download | new post

A PowerShell 2.0 Growl module for Growl For Windows (Updated for PowerShell 2 RTM)

It includes support for adding new apps, new message types, registering scriptblocks to handle Growl’s callbacks, passing URL callbacks, etc.

Original post here and future posts here will explain more about callbacks and sending notices to remote computers.

  1. ## This is the first version of a Growl module (just dot-source to use in PowerShell 1.0)
  2. ## v 1.0 supports a very simple notice, and no callbacks
  3. ## v 2.0 supports registering multiple message types
  4. ##       supports callbacks
  5. ## v 2.1 redesigned to be a module used from apps, rather than it's own "PowerGrowler" app
  6. ## v 3.0 Make it a v2-only PowerShell module
  7. ## v 3.1 fixes for PowerShell 2 RTM
  8. ## TODO:
  9. ## * Test sending notices to other PCs directly
  10.  
  11. Set-StrictMode -Version 2
  12. ## this is just a default now, you'll have opportunities to override it...
  13. $script:appName = "PowerGrowler"
  14.  
  15. [Reflection.Assembly]::LoadFrom("$(Split-Path (gp HKCU:\Software\Growl).'(default)')\Growl.Connector.dll") | Out-Null
  16. if(!(Test-Path Variable:Global:PowerGrowlerNotices)) {
  17.    $global:PowerGrowlerNotices = @{}
  18. }
  19.  
  20. ## We can safely recreate this, it doesn't store much
  21. $script:PowerGrowler = New-Object "Growl.Connector.GrowlConnector"
  22.  
  23. function Register-GrowlType {
  24. #.Synopsis
  25. #  Register a new Type name for growl notices from PowerGrowl
  26. #.Description
  27. #  Creates a new type name that can be used for sending growl notices
  28. #.Parameter AppName
  29. #  The name of the application you want to register as
  30. #.Parameter Name
  31. #  The type name to be used sending growls
  32. #.Parameter DisplayName
  33. #  The test to use for display (defaults to use the same value as the type name)
  34. #.Parameter Icon
  35. #  Overrides the default icon of the message (accepts .ico, .png, .bmp, .jpg, .gif etc)
  36. #.Parameter MachineName
  37. #  The name of a remote machine to register remotely instead of locally.
  38. #.Parameter Priority
  39. #  Overrides the default priority of the message (use sparingly)
  40. #.Example
  41. #  Register-GrowlType "PoshTwitter" "Command Completed"
  42. #  
  43. #  Registers the type "Command Completed," using the default icon, for sending notifications to the local PC
  44. #
  45. PARAM(
  46.    [Parameter(Mandatory=$true,Position=0)]
  47.    [String]$AppName
  48. ,
  49.    [Parameter(Mandatory=$true,Position=1)]
  50.    [ValidateScript( {!$global:PowerGrowlerNotices.Contains($AppName) -OR !$global:PowerGrowlerNotices.$AppName.Notices.ContainsKey($_)} )]
  51.    [String]$Name
  52. ,
  53.    [Parameter(Mandatory=$false,Position=5)]
  54.    [String]$Icon = "$PSScriptRoot\default.ico"
  55. ,
  56.    [Parameter(Mandatory=$false,Position=6)]
  57.    [String]$DisplayName = $Name
  58. ,
  59.    [Parameter(Mandatory=$false,Position=7)]
  60.    [String]$MachineName
  61. ,
  62.    [Parameter(Mandatory=$false)]
  63.    [String]$AppIcon
  64. )
  65.  
  66.    [Growl.Connector.NotificationType]$Notice = $Name
  67.    $Notice.DisplayName = $DisplayName
  68.    $Notice.Icon = Convert-Path (Resolve-Path $Icon)
  69.  
  70.    if($MachineName) {
  71.       $Notice.MachineName = $MachineName
  72.    }
  73.    if(!$global:PowerGrowlerNotices.Contains($AppName)) {
  74.       $global:PowerGrowlerNotices.Add( $AppName, ([Growl.Connector.Application]$AppName) )
  75.  
  76.       $global:PowerGrowlerNotices.$AppName = Add-Member -input $global:PowerGrowlerNotices.$AppName -Name Notices -Type NoteProperty -Value (New-Object hashtable) -Passthru
  77.       if($AppIcon) {
  78.          $global:PowerGrowlerNotices.$AppName.Icon = Convert-Path (Resolve-Path $AppIcon)
  79.       }
  80.    }
  81.    
  82.    $global:PowerGrowlerNotices.$AppName.Notices.Add( $Name, $Notice )
  83.  
  84.    $script:PowerGrowler.Register( $global:PowerGrowlerNotices.$AppName , [Growl.Connector.NotificationType[]]@($global:PowerGrowlerNotices.$AppName.Notices.Values) )
  85. }
  86.  
  87. function Set-GrowlPassword {
  88. #.Synopsis
  89. #  Set the Growl password
  90. #.Description
  91. #  Set the password and optionally, the encryption algorithm, for communicating with Growl
  92. #.Parameter Password
  93. #  The password for Growl
  94. #.Parameter Encryption
  95. #  The algorithm to be used for encryption (defaults to AES)
  96. #.Parameter KeyHash
  97. #  The algorithm to be used for key hashing (defaults to SHA1)
  98. PARAM(
  99.    [Parameter(Mandatory=$true,Position=0)]
  100.    [String]$Password
  101. ,
  102.    [Parameter(Mandatory=$false,Position=1)]
  103.    [ValidateSet( "AES", "DES", "RC2", "TripleDES", "PlainText" )]
  104.    [String]$Encryption = "AES"
  105. ,  
  106.    [Parameter(Mandatory=$false,Position=2)]
  107.    [ValidateSet( "MD5", "SHA1", "SHA256", "SHA384", "SHA512" )]
  108.    [String]$KeyHash = "SHA1"
  109. )  
  110.    $script:PowerGrowler.EncryptionAlgorithm = [Growl.Connector.Cryptography+SymmetricAlgorithmType]::"$Encryption"
  111.    $script:PowerGrowler.KeyHashAlgorithm = [Growl.Connector.Cryptography+SymmetricAlgorithmType]::"$KeyHash"
  112.    $script:PowerGrowler.Password = $Password
  113. }
  114.  
  115. ## Register the "PowerGrowler" "Default" notice so everything works out of the box
  116. Register-GrowlType $script:AppName "Default" -appIcon "$PsScriptRoot\default.ico"
  117.  
  118. function Register-GrowlCallback {
  119. #.Synopsis
  120. #  Register a script to be called when each notice is finished.
  121. #.Description
  122. #  Registers a scriptblock as a handler for the NotificationCallback event. You should accept two parameters, a Growl.Connector.Response and a Growl.Connector.CallbackData object.
  123. #  
  124. #  The NotificationCallback only happens when a callback is requested, which in this Growl library only happens if you pass both CallbackData and CallbackType to the Send-Growl function.
  125. #.Example
  126. #  Register-GrowlCallback { PARAM( $response, $context )
  127. #    Write-Host "Response $($response|out-string)" -fore Cyan
  128. #    Write-Host "Context $($context|fl|out-string)" -fore Green
  129. #    Write-Host $("Response Type: {0}`nNotification ID: {1}`nCallback Data: {2}`nCallback Data Type: {3}`n" -f $context.Result, $context.NotificationID, $context.Data, $context.Type) -fore Yellow
  130. #  }
  131. #
  132. #  Registers an informational debugging-style handler.
  133. #
  134. PARAM(
  135. [Parameter(Mandatory=$true)]
  136. [Scriptblock]$Handler
  137. )
  138.    Register-ObjectEvent $script:PowerGrowler NotificationCallback -Action $Handler
  139. }
  140.  
  141. function Send-Growl {
  142. [CmdletBinding(DefaultParameterSetName="NoCallback")]
  143. #.Synopsis
  144. #  Send a growl notice
  145. #.Description
  146. #  Send a growl notice with the scpecified values
  147. #.Parameter Caption
  148. #  The short caption to display
  149. #.Parameter Message
  150. #  The message to send (most displays will resize to accomodate)
  151. #.Parameter NoticeType
  152. #  The type of notice to send. This MUST be the name of one of the registered types, and senders should bear in mind that each registered type has user-specified settings, so you should not abuse the types, but create your own for messages that will recur.
  153. #  For example, the user settings allow certain messages to be disabled, set to a different "Display", or to have their Duration and Stickyness changed, as well as have them be Forwarded to another device, have Sounds play, and set different priorities.
  154. #.Parameter Icon
  155. #  Overrides the default icon of the message (accepts .ico, .png, .bmp, .jpg, .gif etc)
  156. #.Parameter Priority
  157. #  Overrides the default priority of the message (use sparingly)
  158. #.Example
  159. #  Send-Growl "Greetings" "Hello World!"
  160. #
  161. #  The Hello World of Growl.
  162. #.Example
  163. #  Send-Growl "You've got Mail!" "Message for you sir!" -icon ~\Icons\mail.png
  164. #
  165. #  Displays a message with a couple of movie quotes and a mail icon.
  166. #
  167. PARAM (
  168.    [Parameter(Mandatory=$true, Position=0)]
  169.    [ValidateScript( {$global:PowerGrowlerNotices.Contains($AppName)} )]
  170.    [string]$AppName
  171. ,
  172.    [Parameter(Mandatory=$true, Position=1)][Alias("Type")]  
  173.    [ValidateScript( {$global:PowerGrowlerNotices.$AppName.Notices.ContainsKey($_)} )]  
  174.    [string]$NoticeType
  175. ,
  176.    [Parameter(Mandatory=$true, Position=2)]
  177.    [string]$Caption
  178. ,
  179.    [Parameter(Mandatory=$true, Position=3)]
  180.    [string]$Message
  181. ,
  182.    [Parameter(Mandatory=$true, Position=4, ParameterSetName="UrlCallback")]
  183.    [Uri]$Url
  184. ,  
  185.    [Parameter(Mandatory=$true, Position=4, ParameterSetName="DataCallback")]
  186.    [string]$CallbackData
  187. ,  
  188.    [Parameter(Mandatory=$true, Position=5, ParameterSetName="DataCallback")]
  189.    [string]$CallbackType
  190. ,
  191.    [string]$Icon
  192. ,
  193.    [Growl.Connector.Priority]$Priority = "Normal"
  194. )
  195.  
  196.    $notice = New-Object Growl.Connector.Notification $appName, $NoticeType, (Get-Date).Ticks.ToString(), $caption, $Message
  197.    
  198.    if($Icon) { $notice.Icon = Convert-Path (Resolve-Path $Icon) }
  199.    if($Priority) { $notice.Priority = $Priority }
  200.    
  201.    if($DebugPreference -gt "SilentlyContinue") { Write-Output $notice }
  202.    switch( $PSCmdlet.ParameterSetName ) {
  203.       "UrlCallback" {
  204.          $context = new-object Growl.Connector.CallbackContext $Url
  205.          #  $urlCb = new-object Growl.Connector.UrlCallbackTarget
  206.          #  $urlCb.Url = $Url
  207.          #  $context.SetUrlCallbackTarget($urlcb)
  208.          $script:PowerGrowler.Notify($notice, $context)
  209.          break;
  210.       }
  211.       "DataCallback" {
  212.          $context = new-object Growl.Connector.CallbackContext $CallbackData, $CallbackType
  213.          $script:PowerGrowler.Notify($notice, $context)
  214.          break;
  215.       }
  216.       "NoCallback" {          
  217.          $script:PowerGrowler.Notify($notice)
  218.          break;
  219.       }
  220.    }
  221. }
  222.  
  223. Export-ModuleMember -Function Send-Growl, Set-GrowlPassword, Register-GrowlCallback, Register-GrowlType

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me