用于显示通知气球并采取措施的PowerShell脚本仅在ISE gui中起作用,而不能在命令行中起作用

问题描述

|| 我看过很多密切相关的帖子,所以我知道我并不孤单,但是没人能给我答案。很抱歉,如果有人提出并回答了这个问题,而我找不到它。 此脚本会创建一个自定义通知区域气球,如果单击该气球,则将打开一个指向某些URL的新IE窗口。我已经在其中使用过的PowerShell ISE GUI可以很好地工作。使用我在其他文章中建议的任何选项,都无法使其从命令行运行。具体来说,无法打开IE窗口。通知似乎没有问题,但是没有IE窗口... ??尝试过: 电源外壳 。 script.ps1 powershell-文件script.ps1 命令 和 等等 有什么想法吗? 我的剧本:
#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)
    

解决方法

        它在非交互式提示中不起作用的原因是,当用户单击气球时,powershell已完成处理。 您可以通过以下两种方法之一来解决此问题: 在脚本的末尾添加一个sleep(1),这样它就不会在用户单击气球之前结束; (如果需要,请增加该值,尽管我测试了1秒就可以了) 使用-noexit命令行参数,然后在用户单击通知时或经过一段时间延迟后以编程方式关闭powershell(我测试了它会在不更改代码的情况下启动IE,不会对出口部分进行编码。)     ,        在真正的开发人员的帮助下,更改为C#。希望有一个真正的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
    ,        我相信解决方案是使用
-sta
参数启动
PowerShell.exe
(即控制台窗口)。 Windows GUI代码需要在设置为COM \“ Single Threaded Apartment \”(STA)的线程中运行,但是默认情况下,PowerShell控制台中的工作线程在\“ Multi Threaded Apartment \”(MTA)中运行。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...