function Show-Notification { | |
[cmdletbinding()] | |
Param ( | |
[string] | |
$ToastTitle, | |
[string] | |
[parameter(ValueFromPipeline)] | |
$ToastText | |
) | |
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null | |
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02) | |
$RawXml = [xml] $Template.GetXml() | |
($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null | |
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null | |
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument | |
$SerializedXml.LoadXml($RawXml.OuterXml) | |
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml) | |
$Toast.Tag = "PowerShell" | |
$Toast.Group = "PowerShell" | |
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1) | |
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell") | |
$Notifier.Show($Toast); | |
} |
@e-t-l you'll have to explore the command
schema component for that: https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-command
I'll add this to my TODO list for future updates.
Hi, @dend
I could not use the function when I run the script as a local admin on my computer, whereas it works on my normal domain account.
not sure what permission it would need.
can you please help?
Thank you :)
Exception calling "Show" with "1" argument(s): "Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))"
At line:27 char:5
-
$Notifier.Show($Toast);
-
~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : UnauthorizedAccessException
@jpanda11 this seems related: Windos/BurntToast#27
Doesn't work with PowerShell 7 but does work with Window's built-in version. Unfortunately I'm not familiar with PowerShell enough to suggest an edit. Line 11 appears to be the incompatible line.
Thanks for the heads-up @brainplot - I will have to check it out with PS7.
In case of run on remote computers?
Hi, this script works great for me !
However is there any way to add an icon for the toast ?
I like this function because it's much shorter and simpler than most Toast-Notification-via-Powershell code I've found, but is it possible to add clickable buttons like these (scroll to the "In the Toaster" section)?