用于显示通知气球和执行操作的PowerShell脚本仅适用于ISE gui,而不适用于命令行

时间:2011-03-21 21:48:45

标签: windows-7 powershell notifications powershell-v2.0 powershell-ise

我见过一堆密切相关的帖子所以我知道我并不孤单,但没有人给我答案我正在寻找。如果有人问过并回答了我并且找不到它,那就道歉了。

此脚本会创建一个自定义通知区域气球,如果单击该气球,则会打开一个新的IE窗口到某个URL。从我一直在使用它的PowerShell ISE GUI中工作得很好。无法使用我在其他帖子中看到的任何选项从命令行工作。具体来说,无法打开IE窗口。通知出现没问题,但没有IE窗口......?试过:

  • powershell。 script.ps1
  • powershell -file 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)

3 个答案:

答案 0 :(得分:2)

它在非交互式提示中不起作用的原因是当用户点击气球时,powershell已经完成处理。

您可以通过以下两种方式之一解决问题:

  • 在脚本末尾添加一个sleep(1),因此在用户点击气球之前不会结束; (如果需要,增加值,虽然我测试了1秒就好了)
  • 使用-noexit命令行参数,然后在用户单击通知时或在延迟一段时间之后以编程方式关闭powershell(我测试它会在不更改代码的情况下启动IE,不会编码退出部分。)< / LI>

答案 1 :(得分:0)

在真正的开发人员的帮助下更改为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

答案 2 :(得分:0)

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

Windows GUI代码需要在作为COM“Single Threaded Apartment”(STA)的线程集中运行,但默认情况下,PowerShell控制台中的工作线程正在“多线程单元”(MTA)中运行。