Tuesday, 5 June 2012

Powershell Balloon Notification

Sometimes I need to notify users about something that is happening. It could be an update, an application being deployed or whatever you want.

A fancy way to do it is through the Windows Balloon that pops up in the taskbar.


In the script below I notify the user that new updates are available in the intranet website. Then if the user clicks on the balloon internet explorer opens up in the intranet website. You can adjust it for your requirements.

param
(
    $msg = "For the latest updates check our intranet website. Please click in the balloon.",
    $title = "News",
    $icon = "None",
    $timeout=1
)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-null
$Balloon = new-object System.Windows.Forms.NotifyIcon
$Balloon.Icon = [System.Drawing.SystemIcons]::Information
$Balloon.Visible = $true;
$Balloon.ShowBalloonTip($timeout, $title, $msg, $icon);
Unregister-Event -SourceIdentifier click_event -ErrorAction SilentlyContinue
Register-ObjectEvent $Balloon BalloonTipClicked -sourceIdentifier click_event -Action {
$ie = New-Object -com InternetExplorer.Application
$ie.navigate2("https://intranet.mydomain.com/")
$ie.visible = $true
} | Out-Null
Wait-Event -timeout 15 -sourceIdentifier click_event > $null
Remove-Event click_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier click_event -ErrorAction SilentlyContinue
$Balloon.Dispose()


Just make sure you use the -sta switch when running powershell.
Eg. powershell.exe -sta -file balloon.ps1

4 comments:

  1. Why the needs for -sta??

    ReplyDelete
  2. Hi,

    Because the script creates UI objects and Powershell must be in STA (Single-Threaded Apartment)because most UI components require it. By default Powershell is in MTA (Multi-Threaded Apartment).

    Cheers

    ReplyDelete
  3. Great great idea,

    But I am missing a piece of action: I cannot realize the ballon popping up on the remote computers. What step do I miss?

    Your help is much appreciated

    ReplyDelete
  4. You have to run the script in some way in the remote computer. If you want to run it in a large number of computers then you will have to use GP or something like SCCM.

    ReplyDelete