将通知添加到操作中心的脚本方式?

Scriptable way to add notification to Action Center?

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

是否有 Windows10 等同于将简单通知放入操作中心?

这是你可以使用的东西吗? 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)

首先,将以下函数与 -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)
}

然后您将能够在 Windows10 设置 > 系统 > 通知和操作 > 从这些发件人处获取通知下看到 PowerShell。

单击 PowerShell 并启用 'Show notifications in Action Centre',如下所示:

最后,您可以使用 -ActionCentre 开关调用上述函数以将其发送到那里。