从Powershell中的ScheduledJob触发NotifyIcon

时间:2018-07-06 16:14:27

标签: powershell

考虑以下功能:

function Notify-Windows(
    [Parameter(Mandatory=$true)]
    [string]$text, 
    [string]$icon="Info",
    [string]$title )
{
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
    $objNotifyIcon.Icon = [System.Drawing.SystemIcons]::Information
    $objNotifyIcon.BalloonTipIcon = $icon
    $objNotifyIcon.BalloonTipText = $text
    $objNotifyIcon.BalloonTipTitle = $title
    $objNotifyIcon.Visible = $True
    $objNotifyIcon.ShowBalloonTip(10000)
}

此功能可以正常工作,但是一旦我将其投入预定的工作中,它便不会通知我:

$repeatEvery = New-TimeSpan -Seconds 60
$trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeatEvery
$options = New-ScheduledJobOption -RunElevated
$msg = "Enter the username and password that will run the task";
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)

Register-ScheduledJob -Name notifyWindows -ScriptBlock {
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
    $objNotifyIcon.Icon = [System.Drawing.SystemIcons]::Information
    $objNotifyIcon.BalloonTipIcon = "Info"
    $objNotifyIcon.BalloonTipText = "It worked!"
    $objNotifyIcon.BalloonTipTitle = "My Title"
    $objNotifyIcon.Visible = $True
    $objNotifyIcon.ShowBalloonTip(10000)

    Write-Output "The notify-windows function was called and exited properly"
} `
-Trigger $trigger -ScheduledJobOption $options -Credential $credential

从该代码中,我们得到以下输出:

Id         Name            JobTriggers     Command                                  Enabled
--         ----            -----------     -------                                  -------
120        notifyWindows   1               ...                                      True   

现在,我们抓紧我们关心的工作并运行它:

$sch = Get-ScheduledJob -Id 120
$sch.Run()

作品:

enter image description here

但是现在让我们等待时间表的到来。然后,我们将抓紧工作并进行分析:

> Get-Job | where { $_.Name -eq "notifyWindows" }

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
161    notifyWindows   PSScheduledJob  Completed     True            localhost            ...    

> $job = Get-Job -Id 161
> $job.Output
The notify-windows function was called and exited properly

> $job.Error.Count
0

但仍然...没有通知...

我意识到预定的作业正在单独的Windows会话上运行,但是没有任何方法可以迫使它通知我吗?还是所有用户?

1 个答案:

答案 0 :(得分:0)

好吧,不幸的是,经过多次谷歌搜索之后,我还没有找到一种方法来使用System.Windows.Forms.NotifyIcon进行这项工作。话虽如此,我还是找到了一个名为BurntToast的漂亮小程序包,它工作得很好。

要安装:

> Install-Module -Name BurntToast

,然后更新的实现如下:

$repeatEvery = New-TimeSpan -Seconds 60
$trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeatEvery
$options = New-ScheduledJobOption -RunElevated
$msg = "Enter the username and password that will run the task";
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)

Register-ScheduledJob -Name notifyWindows -ScriptBlock {
    New-BurntToastNotification -Text "It worked!"
    Write-Output "The notify-windows function was called and exited properly"
} `
-Trigger $trigger -ScheduledJobOption $options -Credential $credential 

就是这样。按1分钟的时间表运行,超级简单,OSS ftw。现在,我可以编写作业来监视事物,并在其他事物发生时提醒我!尽管找出为什么我的其他方法无效或至少如何使其有效仍然很高兴。