PowerShell脚本显示通知气球并采取措施仅在ISE GUI中工作,而不是从命令行。

5
我看到了很多相关的帖子,所以知道我不是唯一一个遇到这个问题的人,但是没有一个能给我想要的答案。如果已经有人问过并回答过了,而我没有找到,那么请原谅。
这个脚本创建了一个自定义的通知区域气泡,如果点击它,则应该打开一个新的IE窗口到某个URL。在我使用PowerShell ISE GUI工作时,它非常好用。但是无法通过任何我在其他帖子中看到的选项从命令行中运行。具体来说,无法打开IE窗口。通知没有问题,但没有IE窗口...??尝试过以下方法:
  • powershell . script.ps1
  • powershell -file script.ps1
  • command
  • &

等等。

你有什么想法吗?

我的脚本:

#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个回答

4

在非交互提示中无法使用的原因是当用户点击气球时,powershell已经完成处理。

您可以通过以下两种方式解决:

  • 在脚本末尾添加sleep(1),以便在用户点击气球之前不会结束(如果需要,请增加值,但我测试了1秒就可以);
  • 使用-noexit命令行参数,然后在用户单击通知或在一定延迟后程序化地关闭powershell(我测试过它将启动IE并且不需要更改您的代码,没有编写退出部分)。

2

在一位真正的开发者的帮助下,已经将代码改为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

0

我相信解决方案是使用-sta参数启动PowerShell.exe(即控制台窗口)。

Windows GUI代码需要在一个被设置为COM“单线程公寓”(STA)的线程中运行,但默认情况下,PowerShell控制台中的工作线程正在以“多线程公寓”(MTA)模式运行。


是的,我试过那个了,没有成功,我尝试过这种语法:powershell -Sta \server\share\script.ps1 - Jordan W.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接