以脚本方式向Action Center添加通知?

时间:2015-07-31 17:26:21

标签: windows-10

Mac has a way to post, from command line, a notification

Windows 10是否等同于向操作中心发送简单通知?

2 个答案:

答案 0 :(得分:4)

首先,使用以下函数和-Message生成PowerShell的滑出式Toast通知。

function New-ToastMessage
{
<#
        .SYNOPSIS
        Displays a toast notification with a message and optional image.
        .DESCRIPTION
        Displays a toast notification with a message and optional image.
        .PARAMETER message
        The text message you want to display in your toast.
        .PARAMETER ActionCentre
        Send this to the action centre.
        .PARAMETER image
        An image that you wish to display alongside the message.
        .EXAMPLE
        New-ToastMessage -message "Alert: Disk Space Low (5%)" -image 'C:\Users\Robin\Documents\disk-low.png'
        .EXAMPLE
         New-ToastMessage -message "Alert: Disk Space Low (5%)" -image "C:\Users\Robin\Documents\disk-low.png" -ActionCenter
        .NOTES
        Author: Robin Malik
#>

param(
    [Parameter(Mandatory = $true,HelpMessage = 'Toast Message?')]
    [String]
    $Message,

    [Parameter(HelpMessage = 'Send to action centre')]
    [Switch]
    $ActionCentre,

    [Parameter(Mandatory = $false,HelpMessage = 'Path to image?')]
    [String]
    $Image
)

$ErrorActionPreference = 'Stop'

$notificationTitle = [DateTime]::Now.ToShortTimeString() + ': ' + $Message

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null

if($Image)
{
    $templateType = 'ToastImageAndText01'
}
else
{
    $templateType = 'ToastText01'
}

$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::$templateType)

#Convert to .NET type for XML manipuration
$toastXml = [xml]$template.GetXml()

if($Image)
{
    $toastXml.GetElementsByTagName('image').SetAttribute('src',$Image) > $null
    $toastXml.GetElementsByTagName('image').SetAttribute('alt','overlay text') > $null
}
$toastXml.GetElementsByTagName('text').AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null

#Convert back to WinRT type
$xml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)

$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = 'PowerShell'
$toast.Group = 'PowerShell'
$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)
if($actioncentre)
{
    $toast.SuppressPopup = $true
}
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('PowerShell')
$notifier.Show($toast)
}

然后,您将能够在Windows 10设置&gt;下看到PowerShell。系统&gt;通知&amp;行动&gt;从这些发件人处获取通知。

点击PowerShell并启用&#39;在操作中心显示通知&#39;,如下所示:

PowerShell notifications area for Windows 10

最后,你可以用-ActionCentre开关调用上面的函数来代替它。

答案 1 :(得分:2)

这可以用吗? https://technet.microsoft.com/en-us/library/ff730952.aspx

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon 

$objNotifyIcon.Icon = "C:\Scripts\Forms\Folder.ico"
$objNotifyIcon.BalloonTipIcon = "Error" 
$objNotifyIcon.BalloonTipText = "A file needed to complete the operation could not be found." 
$objNotifyIcon.BalloonTipTitle = "File Not Found"

$objNotifyIcon.Visible = $True 
$objNotifyIcon.ShowBalloonTip(10000)