PowerShell脚本来显示通知气球和采取行动只能在ISE gui,而不是从命令行
问题描述:
我见过一堆密切相关的职位,所以我知道我并不孤单,但没有人给了我答案我是寻找。道歉,如果这已被要求,并回答了,我找不到它。PowerShell脚本来显示通知气球和采取行动只能在ISE gui,而不是从命令行
此脚本创建一个自定义通知区域气球,如果点击它就意味着打开一个新的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)
答
为什么它不能在非交互式提示工作的原因是,当用户点击气球的PowerShell已经完成处理。
您可以修复,在以下两种方法之一:
- (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
答
我认为解决的办法就是启动PowerShell.exe
(即控制台窗口。)与-sta
参数。
Windows GUI代码需要在作为COM“单线程公寓”(STA)的线程集中运行,但默认情况下,PowerShell控制台中的工作线程正在“多线程公寓”(MTA)中运行。
是的我试过这一个,没有运气,试着用这个语法:powershell -Sta \\ server \ share \ script.ps1 – 2011-03-22 20:32:14