Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

windows 10 - Scriptable way to add notification to Action Center?

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

Is there a Windows 10 equivalent to putting a simple notification into the Action Center?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, use the following function with -Message to generate a slide out toast notification for PowerShell.

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:UsersRobinDocumentsdisk-low.png'
        .EXAMPLE
         New-ToastMessage -message "Alert: Disk Space Low (5%)" -image "C:UsersRobinDocumentsdisk-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)
}

You will then be able to see PowerShell under the Windows 10 Settings > System > Notifications & actions > Get Notifications from these senders.

Click PowerShell and enable 'Show notifications in Action Centre', like so:

PowerShell notifications area for Windows 10

Finally, you can call the above function with the -ActionCentre switch to send it there instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...