PoshCode Logo PowerShell Code Repository

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

This is the PowerShell 2.0 -only continuation of my Growl module

It includes preliminary support for adding new message types, and registering scriptblocks to handle Growl’s callbacks, as well as messaging remote computers.

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. ## Initially it only supports a very simple notice, and I haven't gotten callbacks working yet
  3. ## Coming soon:
  4. ## * Send notices to other PCs directly
  5. ## * Wrap the registration of new messages
  6. ## * Figure out the stupid
  7.  
  8. ## Change these to whatever you like, at least the first one, since it should point at a real ico file
  9. $appName = "PowerGrowler"
  10.  
  11. [Reflection.Assembly]::LoadFrom("$(Split-Path (gp HKCU:\Software\Growl).'(default)')\Growl.Connector.dll") | Out-Null
  12.  
  13. if(!(Test-Path Variable:Global:PowerGrowlerNotices)) {
  14.    $global:PowerGrowlerNotices = @{}
  15. }
  16.  
  17. ## We can safely recreate these, they don't do much
  18. [Growl.Connector.Application]$script:GrowlApp = $appName
  19. $script:GrowlApp.Icon = Convert-Path (Resolve-Path "$PSScriptRoot\default.ico")
  20. $script:PowerGrowler = New-Object "Growl.Connector.GrowlConnector"
  21.  
  22.  
  23. function Set-GrowlPassword {
  24. #.Synopsis
  25. #  Set the Growl password
  26. #.Description
  27. #  Set the password and optionally, the encryption algorithm, for communicating with Growl
  28. #.Parameter Password
  29. #  The password for Growl
  30. #.Parameter Encryption
  31. #  The algorithm to be used for encryption (defaults to AES)
  32. #.Parameter KeyHash
  33. #  The algorithm to be used for key hashing (defaults to SHA1)
  34. PARAM(
  35.    [Parameter(Mandatory=$true,Position=0)]
  36.    [String]$Password
  37. ,
  38.    [Parameter(Mandatory=$false,Position=1)]
  39.    [ValidateSet( "AES", "DES", "RC2", "TripleDES", "PlainText" )]
  40.    [String]$Encryption = "AES"
  41. ,  
  42.    [Parameter(Mandatory=$false,Position=2)]
  43.    [ValidateSet( "MD5", "SHA1", "SHA256", "SHA384", "SHA512" )]
  44.    [String]$KeyHash = "SHA1"
  45. )  
  46.    $script:PowerGrowler.EncryptionAlgorithm = [Growl.Connector.Cryptography+SymmetricAlgorithmType]::"$Encryption"
  47.    $script:PowerGrowler.KeyHashAlgorithm = [Growl.Connector.Cryptography+SymmetricAlgorithmType]::"$KeyHash"
  48.    $script:PowerGrowler.Password = $Password
  49. }
  50.  
  51. function Register-GrowlType {
  52. #.Synopsis
  53. #  Register a new Type name for growl notices from PowerGrowl
  54. #.Description
  55. #  Creates a new type name that can be used for sending growl notices
  56. #.Parameter Name
  57. #  The type name to be used sending growls
  58. #.Parameter DisplayName
  59. #  The test to use for display (defaults to use the same value as the type name)
  60. #.Parameter Icon
  61. #  Overrides the default icon of the message (accepts .ico, .png, .bmp, .jpg, .gif etc)
  62. #.Parameter MachineName
  63. #  The name of a remote machine to register remotely instead of locally.
  64. #.Parameter Priority
  65. #  Overrides the default priority of the message (use sparingly)
  66. #.Example
  67. #  Register-GrowlType "Command Completed"
  68. #  
  69. #  Registers the type "Command Completed," using the default icon, for sending notifications to the local PC
  70. #
  71. PARAM(
  72.    [Parameter(Mandatory=$true,Position=0)]
  73.    [ValidateScript( {!$global:PowerGrowlerNotices.ContainsKey($_)} )]
  74.    [String]$Name
  75. ,
  76.    [Parameter(Mandatory=$false,Position=1)]
  77.    [String]$Icon
  78. ,
  79.    [Parameter(Mandatory=$false,Position=2)]
  80.    [String]$DisplayName = $NoticeName
  81. ,
  82.    [Parameter(Mandatory=$false,Position=3)]
  83.    [String]$MachineName
  84. )
  85.  
  86.    [Growl.Connector.NotificationType]$Notice = $Name
  87.    $Notice.DisplayName = $DisplayName
  88.    if($Icon) {
  89.       $Notice.Icon = Convert-Path (Resolve-Path $Icon)$Icon
  90.    }
  91.    if($MachineName) {
  92.       $Notice.MachineName = $MachineName
  93.    }
  94.    
  95.    $global:PowerGrowlerNotices.Add( $Name, $Notice )
  96.    $script:PowerGrowler.Register( $script:GrowlApp, @($global:PowerGrowlerNotices.Values) )
  97. }
  98.  
  99.  
  100.  
  101. ## Register the "Default" notice type, just so everything works out of the box
  102. Register-GrowlType "Default"
  103.  
  104. function Register-GrowlCallback {
  105. #.Synopsis
  106. #  Register a script to be called when each notice is finished.
  107. #.Description
  108. #  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.
  109. #  
  110. #  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.
  111. #.Example
  112. #  Register-GrowlCallback { PARAM( $response, $context )
  113. #    Write-Host "Response $($response|out-string)" -fore Cyan
  114. #    Write-Host "Context $($context|gm|out-string)" -fore Green
  115. #    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
  116. #  }
  117. #
  118. #  Registers an informational debugging-style handler.
  119. #
  120. PARAM(
  121. [Parameter(Mandatory=$true)]
  122. [Scriptblock]$Handler
  123. )
  124.    Register-ObjectEvent $script:PowerGrowler NotificationCallback -Action $Handler
  125. }
  126.  
  127. function Send-Growl {
  128. #.Synopsis
  129. #  Send a growl notice
  130. #.Description
  131. #  Send a growl notice with the scpecified values
  132. #.Parameter Caption
  133. #  The short caption to display
  134. #.Parameter Message
  135. #  The message to send (most displays will resize to accomodate)
  136. #.Parameter NoticeType
  137. #  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.
  138. #  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.
  139. #.Parameter Icon
  140. #  Overrides the default icon of the message (accepts .ico, .png, .bmp, .jpg, .gif etc)
  141. #.Parameter Priority
  142. #  Overrides the default priority of the message (use sparingly)
  143. #.Example
  144. #  Send-Growl "Greetings" "Hello World!"
  145. #
  146. #  The Hello World of Growl.
  147. #.Example
  148. #  Send-Growl "You've got Mail!" "Message for you sir!" -icon ~\Icons\mail.png
  149. #
  150. #  Displays a message with a couple of movie quotes and a mail icon.
  151. #
  152. PARAM (
  153.    [Parameter(Mandatory=$true, Position=0)]
  154.    [string]$Caption
  155. ,
  156.    [Parameter(Mandatory=$true, Position=1)]
  157.    [string]$Message
  158. ,  
  159.    [Parameter(Mandatory=$false, Position=2)]
  160.    [string]$CallbackData
  161. ,  
  162.    [Parameter(Mandatory=$false, Position=3)]
  163.    [string]$CallbackType
  164. ,
  165.    [Parameter(Mandatory=$false)][Alias("Type")]  
  166.    [ValidateScript( {$global:PowerGrowlerNotices.ContainsKey($_)} )]
  167.    [string]$NoticeType="Default"
  168. ,
  169.    [string]$Icon
  170. ,
  171.    [Growl.Connector.Priority]$Priority = "Normal"
  172. )
  173.  
  174.    $notice = New-Object Growl.Connector.Notification $appName, $NoticeType, (Get-Date).Ticks.ToString(), $caption, $Message
  175.    
  176.    if($Icon) { $notice.Icon = Convert-Path (Resolve-Path $Icon) }
  177.    if($Priority) { $notice.Priority = $Priority }
  178.    
  179.    if($DebugPreference -gt "SilentlyContinue") { Write-Output $notice }
  180.    if( $CallbackData -and $CallbackType ) {
  181.       $context = new-object Growl.Connector.CallbackContext
  182.       $context.Data = $CallbackData
  183.       $context.Type = $CallbackType
  184.       $script:PowerGrowler.Notify($notice, $context)
  185.    } else {
  186.       $script:PowerGrowler.Notify($notice)
  187.    }
  188. }
  189.  
  190. 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