PowerShell script to display notification ball and action only works in ISE gui, not from the command line

I saw a bunch of closely related posts, so I know that I'm not alone, but no one has given me the answer I'm looking for. I apologize if they asked and answered about this, and I could not find him.

this script creates a custom notification area balloon, which, when clicked, is intended to open a new IE window for some URL. Works great with the PowerShell ISE GUI that I worked with. I can not get it to work from the command line using any parameters that I saw in other posts. In particular, IE cannot open the window for . The notification does not cause any problems, but there is no IE window ... ?? Tried using:

  • powershell. script.ps1
  • powershell -file script.ps1
  • Team
  • &

and etc.

Thoughts?

My script:

#Load the required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Remove any registered events related to notifications
Remove-Event BalloonClicked_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
Remove-Event BalloonClosed_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue
Remove-Event Disposed -ea SilentlyContinue
Unregister-Event -SourceIdentifier Disposed -ea silentlycontinue

#Create the notification object
$notification = New-Object System.Windows.Forms.NotifyIcon 
#Define various parts of the notification
$notification.Icon = [System.Drawing.SystemIcons]::Information
$notification.BalloonTipTitle = "**Reminder**"
$notification.BalloonTipIcon = "Warning"
$title = "message to user"
$notification.BalloonTipText = $title

#Make balloon tip visible when called
$notification.Visible = $True

## Register a click event with action to take based on event
#Balloon message clicked
register-objectevent $notification BalloonTipClicked BalloonClicked_event -Action {
    Start-Process 'c:\Program Files\Internet Explorer\iexplore.exe' -ArgumentList 'http://someURL.com' -WindowStyle Maximized -Verb Open
    #Get rid of the icon after action is taken
    $notification.Dispose()
    } | Out-Null

#Balloon message closed
register-objectevent $notification BalloonTipClosed BalloonClosed_event -Action {$notification.Dispose()} | Out-Null

#Call the balloon notification
$notification.ShowBalloonTip(1000)
+3
source share
3 answers

The reason it doesn't work on non-interactive prompts is because powershell has already completed processing when the user clicks on the balloon.

You can fix this in one of two ways:

  • add sleep (1) to the end of the script so that it does not end until the user clicks on the ball; (increase the value if necessary, although I tested w / 1 sec just fine).
  • -noexit PowerShell ( , IE - , .)
+2

# . PowerShell, , - .

:

$code = @'
using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        NotifyIcon nicon = new NotifyIcon();
        nicon.BalloonTipIcon = ToolTipIcon.Info;
        nicon.BalloonTipText = "message to user";
        nicon.BalloonTipTitle = "*** title for user ***";
        nicon.Icon = SystemIcons.Information;
        nicon.BalloonTipClicked += (sender, e) =>
        {
            System.Diagnostics.Process.Start("http://website.com");
            CleanUp(nicon);
        };
        nicon.BalloonTipClosed += (sender, e) => CleanUp(nicon);
        nicon.Visible = true;
        nicon.ShowBalloonTip(1000 * 1);
        Application.Run();
    }
    static void CleanUp(NotifyIcon c)
    {
        c.Visible = false;
        c.Dispose();
        Application.Exit();
    }
}

'@
Write-Host $code
Add-Type -OutputType WindowsApplication -OutputAssembly c:\temp\test.exe -TypeDefinition  $code -ReferencedAssemblies "System.Drawing","System.Windows.Forms" -Language CSharpVersion3
0

, , PowerShell.exe ( ) -sta.

GUI Windows , COM "Single Threaded Apartment" (STA), PowerShell "Multi Threaded Apartment" (MTA).

0
source

All Articles