none
Powershell notification with an icon stuck and I need it to have a click action. RRS feed

  • Question

  • I have this Powershell script that I use with a batch file to put up a notification. I just want it to do something when the user clicks, and I want the icon that gets stuck on the taskbar to go away. 

    Note: I can add images of the notification and icon once my account is verified.

    Here's the script: It runs with no errors as long as you have an icon named icon.ico:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon   
    $objNotifyIcon.Icon = "icon.ico"  
    $objNotifyIcon.BalloonTipIcon = "None"   
    $objNotifyIcon.BalloonTipText = "Hello There Person, click me to open CMD"   
    $objNotifyIcon.BalloonTipTitle = "Hello"
    $objNotifyIcon.Visible = $True   
    $objNotifyIcon.ShowBalloonTip(10000)
    
    
    function Show-BalloonTip {            
    [cmdletbinding()]            
    param(            
     [parameter(Mandatory=$true)]            
     [string]$Title,            
     [ValidateSet("Info","Warning","Error")]             
     [string]$MessageType = "Info",            
     [parameter(Mandatory=$true)]            
     [string]$Message,            
     [string]$Duration=10000            
    )            
    
    [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null            
    $balloon = New-Object System.Windows.Forms.NotifyIcon            
    $path = Get-Process -id $pid | Select-Object -ExpandProperty Path            
    $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)            
    $balloon.Icon = $icon            
    $balloon.BalloonTipIcon = $MessageType            
    $balloon.BalloonTipText = $Message            
    $balloon.BalloonTipTitle = $Title            
    $balloon.Visible = $true            
    $balloon.ShowBalloonTip($Duration)            
    
    }

    Thanks for any help.

    • Moved by Bill_Stewart Wednesday, May 9, 2018 2:38 PM This is not code customization service
    Friday, March 16, 2018 5:23 PM

All replies

  • Here is an article discussing how to use the NotifyIcon control.

    https://mcpmag.com/articles/2017/09/07/creating-a-balloon-tip-notification-using-powershell.aspx

    If you search you will find many articles showing different ways to script this.


    \_(ツ)_/

    Friday, March 16, 2018 7:00 PM
  • Here is the correctl way to use the code you copied form various places.  

    function Show-BalloonTip {            
        param(            
         [parameter(Mandatory=$true)]            
         [string]$Title,            
         [ValidateSet("Info","Warning","Error")]             
         [string]$MessageType = "Info",            
         [parameter(Mandatory=$true)]            
         [string]$Message,            
         [string]$Duration=10000            
        )            
        Add-Type -AssemblyName System.Windows.Forms
        $balloon = New-Object System.Windows.Forms.NotifyIcon            
        $path = Get-Process -id $pid | Select-Object -ExpandProperty Path            
        $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)            
        $balloon.Icon = $icon            
        $balloon.BalloonTipIcon = $MessageType            
        $balloon.BalloonTipText = $Message            
        $balloon.BalloonTipTitle = $Title            
        $balloon.Visible = $true
        $balloon.add_BalloonTipClicked({Write-Host 'you clicked me1!' -fore Green}) $balloon.ShowBalloonTip($Duration) } Show-BalloonTip -Title 'Hello' -MessageType Info -Message 'Hello There Person, click me to open CMD'
    Now just add an event to the balloon tip and you will be set.  First take a bit of time to learn PowerShell.  Advanced things like forms will be impossible without learning PowerShell.


    \_(ツ)_/


    • Edited by jrv Friday, March 16, 2018 9:31 PM
    Friday, March 16, 2018 9:26 PM
  • Thank you jrv, now to be honest I don't know powershell. My coders are away for St. Patrick's day. Could you give me an example of an event? Also it is no longer using a custom icon. Sorry to be a pain, usually I'd have someone else look at this but they're not great with Powershell.
    Saturday, March 17, 2018 12:07 PM
  • Currently its giving me this error:

    Unhandled exception has occurred in a component of your application. If you click continue, the application will ignore this error and attempt to continue. There is no runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: Write-Host 'you...1!' -force Green


    with these details: 

    See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.
    
    ************** Exception Text **************
    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: Write-Host 'you...1!' -fore Green
       at System.Management.Automation.ScriptBlock.GetContextFromTLS()
       at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
       at System.Windows.Forms.NotifyIcon.OnBalloonTipClicked()
       at System.Windows.Forms.NotifyIcon.WndProc(Message& msg)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
    ----------------------------------------
    powershell_ise
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15 (WinBuild.160101.0800)
        CodeBase: file:///C:/Windows/System32/WindowsPowerShell/v1.0/powershell_ise.exe
    ----------------------------------------
    Microsoft.PowerShell.ISECommon
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.ISECommon/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.ISECommon.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Management.Automation
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.251
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Management.Automation/v4.0_3.0.0.0__31bf3856ad364e35/System.Management.Automation.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2612.0 built by: NET471REL1LAST_B
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    Microsoft.PowerShell.GPowerShell
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.GPowerShell/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.GPowerShell.dll
    ----------------------------------------
    System.ComponentModel.Composition
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.ComponentModel.Composition/v4.0_4.0.0.0__b77a5c561934e089/System.ComponentModel.Composition.dll
    ----------------------------------------
    Microsoft.PowerShell.Editor
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Editor/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Editor.dll
    ----------------------------------------
    WindowsBase
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
    ----------------------------------------
    PresentationFramework
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll
    ----------------------------------------
    PresentationCore
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll
    ----------------------------------------
    System.Xaml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll
    ----------------------------------------
    System.Runtime.Serialization
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2612.0 built by: NET471REL1LAST_B
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
    ----------------------------------------
    UIAutomationProvider
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/UIAutomationProvider/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationProvider.dll
    ----------------------------------------
    Accessibility
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
    ----------------------------------------
    MetadataViewProxies_cba1beb4-13f4-40f5-b05e-f03dad0291d7
        Assembly Version: 0.0.0.0
        Win32 Version: 4.7.2556.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.ComponentModel.Composition/v4.0_4.0.0.0__b77a5c561934e089/System.ComponentModel.Composition.dll
    ----------------------------------------
    System.Management
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Management/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Management.dll
    ----------------------------------------
    System.DirectoryServices
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.DirectoryServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.dll
    ----------------------------------------
    Microsoft.Management.Infrastructure
        Assembly Version: 1.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Management.Infrastructure/v4.0_1.0.0.0__31bf3856ad364e35/Microsoft.Management.Infrastructure.dll
    ----------------------------------------
    PresentationFramework.AeroLite
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.AeroLite/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.AeroLite.dll
    ----------------------------------------
    PresentationFramework-SystemXml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXml/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXml.dll
    ----------------------------------------
    System.Data
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    Anonymously Hosted DynamicMethods Assembly
        Assembly Version: 0.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
    ----------------------------------------
    Microsoft.PowerShell.Security
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Security/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Security.dll
    ----------------------------------------
    System.Transactions
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
    ----------------------------------------
    System.Numerics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
    ----------------------------------------
    Microsoft.PowerShell.GraphicalHost
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.GraphicalHost/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.GraphicalHost.dll
    ----------------------------------------
    UIAutomationTypes
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2633.0 built by: NET471REL1LAST_C
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
    ----------------------------------------
    Microsoft.GeneratedCode
        Assembly Version: 1.0.0.0
        Win32 Version: 4.7.2612.0 built by: NET471REL1LAST_B
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    PresentationFramework-SystemData
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemData/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemData.dll
    ----------------------------------------
    PresentationFramework-SystemCore
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemCore/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemCore.dll
    ----------------------------------------
    Windows.UI
        Assembly Version: 255.255.255.255
        Win32 Version: 10.0.10011.16384
        CodeBase: file:///C:/WINDOWS/system32/WinMetadata/Windows.UI.winmd
    ----------------------------------------
    System.Runtime.InteropServices.WindowsRuntime
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.InteropServices.WindowsRuntime/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Runtime.InteropServices.WindowsRuntime.dll
    ----------------------------------------
    Microsoft.PowerShell.Commands.Utility
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Commands.Utility/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Commands.Utility.dll
    ----------------------------------------
    System.Configuration.Install
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2556.0 built by: NET471REL1
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration.Install/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.Install.dll
    ----------------------------------------
    Microsoft.PowerShell.Commands.Management
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.16299.15
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Commands.Management/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Commands.Management.dll
    ----------------------------------------
    SMDiagnostics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2612.0 built by: NET471REL1LAST_B
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/SMDiagnostics/v4.0_4.0.0.0__b77a5c561934e089/SMDiagnostics.dll
    ----------------------------------------
    System.ServiceModel.Internals
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2612.0 built by: NET471REL1LAST_B
        CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Internals/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Internals.dll
    ----------------------------------------
    
    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.
    
    For example:
    
    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>
    
    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.
    
    
    

    • Edited by ITCMD Lucas Saturday, March 17, 2018 12:11 PM editing
    Saturday, March 17, 2018 12:07 PM
  • Please note that the icon for powershell still remains in the taskbar until a mouse goes over it too. Sorry to be such a pain!
    Saturday, March 17, 2018 12:14 PM
  • You will need to get someone who knows PowerShell to help you.  This is not an end suer support forum.  The assumption is that you are a PowerShell scripter or are learning and asking a question about PowerShell.


    \_(ツ)_/

    Saturday, March 17, 2018 12:16 PM
  • Please note that the icon for powershell still remains in the taskbar until a mouse goes over it too. Sorry to be such a pain!

    A NotifyIcon remains for the life of the process that created it unless the process destroys the icon.

    I posted an example of adding an event above.

    Since you are not a technical person all of this will be very hard for you to understand.  Wait for your techs to come back.  They might be able to help you


    \_(ツ)_/

    Saturday, March 17, 2018 12:21 PM
  • The code you posted and that I fixed was from an old blog post.  he author did not understand how to use this resource.  The code will display a message and it can be clicked.  Every time it is run a new notify icon will be created.  This is not how the object is designed to work so it will not work well.

    Here is the documentation and a complete example of how to set this up.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon(v=vs.110).aspx

    There ae no good examples of this in PowerShell that I have found except this post: https://www.sapien.com/blog/2012/05/08/spotlight-on-the-notifyicon-control/

      


    \_(ツ)_/

    Saturday, March 17, 2018 12:47 PM
  • Here is a complete PowerShell example: https://1drv.ms/u/s!AjiiPtIUqzK_hdZOEWUaTIwEkmFO7g


    \_(ツ)_/

    Saturday, March 17, 2018 12:49 PM