PS: Working with balloon tips

I have a Powershell function that shows a balloon tip and it works fine, except for two things. First, is there a way to have my script display a message to every logged-in user? 

Second, according to several blog posts (like this and this), I should be cleaning up my icon. The sample code doesn't seem to work for me. Regardless of how I click the balloon, the icon says in place. If I hover my mouse over the icon (with or without closing the balloon), the icon disappears. Am I missing something here?

Thanks.

Function Start-LogoffNotice {
    <#
    .SYNOPSIS
        Displays a notice in the Windows notification area.
    #>
    param (
        $IconPath = (Get-Process -id $pid | Select-Object -ExpandProperty Path),

        [ValidateSet("Info","Warning","Error","None")]
        $BalloonIcon,
        
        $BalloonText,

        $BalloonTitle,
        
        $BalloonTimeout = "60000"
    )

        Add-Type -AssemblyName System.Windows.Forms | Out-Null
        $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 

        $objNotifyIcon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($IconPath)
        $objNotifyIcon.BalloonTipIcon = $BalloonIcon
        $objNotifyIcon.BalloonTipText = $BalloonText
        $objNotifyIcon.BalloonTipTitle = $BalloonTitle
        $objNotifyIcon.Visible = $True 
        $objNotifyIcon.ShowBalloonTip($BalloonTimeout)
        
        #This section doesn't seem to do anything.
        Register-ObjectEvent -InputObject $objNotifyIcon -EventName BalloonTipClosed -Action {                     
            Unregister-Event $objNotifyIcon.SourceIdentifier
            Remove-Job $objNotifyIcon.Action
            $objNotifyIcon.Dispose()
        } | Out-Null
    }

Start-LogoffNotice -BalloonIcon "Info" -BalloonText "You will be logged off in 1 minute. Please save your work." -BalloonTitle "Logoff Notice"

February 14th, 2015 1:04am

Regarding your second question, you need you make sure the $objNotifyIcon.Dispose() method is being run. That's the piece that will remove the icon from the Notification Area. Perhaps you can copy it from the section that indicates it doesn't do anything and paste it underneath $objNotifyIcon.ShowBalloonTip($BalloonTimeout). If it disposes right away, then you may need to add a Start-Sleep command between the two, such as Start-Sleep -Milliseconds $BalloonTimeout.

Edit: Rewording for clarity.


Free Windows Admin Tool Kit Click here and download it now
February 14th, 2015 1:24am

The script is OK but the dispose will only be called if the user events the icon.

If you script exits then nothing will happen - as you have seen.

February 14th, 2015 4:37am

One other thing. I don't think you cannot dispose of an object when you are in the object.

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2015 4:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics