none
Powershell Notification Icon - Show balloon when clicked RRS feed

  • Question

  • Hi

    I managed to create a notification icon in the task bar that changes based on Status of VPN connection.

    The icon will stay in the taskbar at all times, so I would like the user to be able to get the balloon tip again when the mouse goes over the icon or when the user clicks it.

    So far I used code that shows a messagebox. It works fine but only once. Ideally it should be available at all times.

    here is the code.

    Thanks in advance for suggestions.

    PROCESS {
    if ($vstate -eq 1) {
    $errlevel = "Information"
    $constatetext = "VPN is Connected"
    $constateicon = "imageres_106.ico"
    }
    else
    {
    $errlevel = "Warning"
    $constatetext = "VPN is not Connected"
    $constateicon = "imageres_107.ico"
    }
    Add-Type -AssemblyName System.Windows.Forms
    if ($script:balloon -eq $null)
     {
      $script:balloon = New-Object System.Windows.Forms.NotifyIcon
     }
      $balloon.Icon            = $constateicon
      
    $balloon.BalloonTipIcon  = $errlevel
      $balloon.BalloonTipText  = $constatetext
      $balloon.BalloonTipTitle = "Citrix Secure Access VPN"
      $balloon.Visible         = $true
      $balloon.ShowBalloonTip($Timeout)
     
     register-objectevent $balloon Click Click_event `
     -Action {[System.Windows.Forms.MessageBox]::Show(“Balloon message clicked”,”Information”);$notification.Visible = $False} | Out-Null
     
    
    }


    bruno

    Thursday, April 10, 2014 8:43 AM

Answers

  • Sorry but you cannot do that.  The events are sent to the parent form's message loop.  You have no form.  Therefore you have no mouse events.  It I not possible to do what you are trying to do.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, April 11, 2014 9:24 AM
    • Marked as answer by brpo Friday, April 11, 2014 9:30 AM
    Friday, April 11, 2014 9:23 AM

All replies

  • Hi Bruno,

    the form NotifyIcon has various Events that can help you with that. Click, Mouseclick or mousemove might be the events you want.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, April 10, 2014 10:10 AM
  • Hi

    thanks for feedback. I would like to use the MouseMove event to display the $constatetext

    However I do not know what is the syntax for the Action

    register-objectevent $balloon MouseMove MouseMove_event -Action {}

    Also, is there a way to make the event available at all times (I guess with a kind of loop).

    I try to have the user be able to check the vpn state, the script runs in the background but

    in the current state the event can only be called once.

    thanks in advance


    bruno

    Thursday, April 10, 2014 3:28 PM
  • Just add this to the event on the notifyIcon.

    $notifyicon1_MouseMove=[System.Windows.Forms.MouseEventHandler]{
         $notifyicon1.ShowBalloonTip(3)
    }
    $notifyicon1.add_MouseMove($notifyicon1_MouseMove)


    ¯\_(ツ)_/¯

    Friday, April 11, 2014 5:00 AM
  • Hi

    thanks for the code snippet,

    I tried but I get following error

    System.Management.Automation.PSInvalidOperationException: There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was:
    $balloon.ShowBalloonTip(3)

    here is the code I use below

     register-objectevent $balloon Click Click_Event -Action {[System.Windows.Forms.MessageBox]::Show(“Balloon message clicked”,”Information”);$notification.Visible = $False} 
    $balloon_MouseMove=[System.Windows.Forms.MouseEventHandler]{
    $balloon.ShowBalloonTip(3)
    }
    $balloon.add_MouseMove($Balloon_MouseMove)
    
    


    bruno

    Friday, April 11, 2014 8:17 AM
  • I didn't say to register an event.  Just add the code to the form.  You cannot run a control without a form.  The console is not a real Window. YOu cannot register control events in PowerShell.

    ¯\_(ツ)_/¯

    Friday, April 11, 2014 8:30 AM
  • Hi,

    Thanks again for your patience. 

    Just to clarify, this code checks the state of the Citrix Secure access Client in order to detect when the VPN is connected and based on this, launch several actions. One of those is to show an icon in the taskbar and display a Balloontip when connectivity changes so that the users always have this information available.

    I just wanted to show also the balloontip when the user hovers over the icon with the mouse. I got to the code below that works but launches only once, so I was wondering if the code could be adapted to show the balloontip instead of msgbox and to be always available instead of unloading after first occurrence.

    register-objectevent $balloon Click Click_Event

    -Action {[System.Windows.Forms.MessageBox]::Show

    (“Balloon message clicked”,”Information”)

    ;$notification.Visible = $False} | Out-Null

    Here  is the functon so far.

    Function Show-Icon { [CmdletBinding()] param( [Parameter(Mandatory=$False,ValueFromPipeline=$True)] [string]$vstate, [string]$ErrorLog = "c:\windows\temp\vpnscriptERRlog.log", [string]$RunLog = "c:\windows\temp\vpnscriptrun.log" ) BEGIN { $Exetime = Get-date Add-content -value $Exetime -Path $Runlog write-output $vstate $timeout = 2000 } PROCESS { if ($vstate -eq 1) { $errlevel = "Information" $constatetext = "VPN is Connected" $constateicon = "imageres_106.ico" } else { $errlevel = "Warning" $constatetext = "VPN is not Connected" $constateicon = "imageres_107.ico" } Add-Type -AssemblyName System.Windows.Forms if ($script:balloon -eq $null) { $script:balloon = New-Object System.Windows.Forms.NotifyIcon } $balloon.Icon = $constateicon $balloon.BalloonTipIcon = $errlevel $balloon.BalloonTipText = $constatetext $balloon.BalloonTipTitle = "Citrix Secure Access VPN" $balloon.Visible = $true $balloon.ShowBalloonTip($Timeout) } END { } }

    brgds



    bruno

    Friday, April 11, 2014 9:14 AM
  • Sorry but you cannot do that.  The events are sent to the parent form's message loop.  You have no form.  Therefore you have no mouse events.  It I not possible to do what you are trying to do.


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, April 11, 2014 9:24 AM
    • Marked as answer by brpo Friday, April 11, 2014 9:30 AM
    Friday, April 11, 2014 9:23 AM
  • Ok

    Thanks again for your help.

    Bruno


    bruno

    Friday, April 11, 2014 9:30 AM