-
-
Save dend/5ae8a70678e3a35d02ecd39c12f99110 to your computer and use it in GitHub Desktop.
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 ?
@dend Did you find a workaround for PS7?
As far as I know, it's not possible with default PS7..
Agree. Doesn't work with PS7.
Does this script have a license? I'd like to try to play around with it and maybe use it a little, but don't know the terms and conditions
@houstdav000 - the license is "Use and remix this for whatever you want" 😊
@Hrxn @TotallyInformation haven't gotten a chance to write anything for PS7 yet. I'll have to test it on another box potentially later in the week and see where I can get with it.
Thanks Den, looking forward to using it 😁
Did some digging - this is related to PowerShell/PowerShell#13042.
To mitigate the issue, download this package: https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/#readme-body-tab
Then, you can add the assemblies from PS7:
Add-Type -AssemblyName WinRT.Runtime.dll
Add-Type -AssemblyName Microsoft.Windows.SDK.NET.dll
Thanks for digging into that. Sadly, the resolution appears to be more complex than is worth the effort! I'd need to install other apps to manage a nuget package then work out where I'd want the libraries and how to make sure they were kept current. Kind of the point of a toast message is simplicity! 🤣
When Microsoft (or some other kind person/group) have figured out how to use their own libraries with their own modern software, I'll revisit. Until then, I'll use alternatives.
@dend how to prevent the previous toast notifications from being overwritten by new toast in action center? I want to keep all previous toasts and disappear only when I close it from action center
Here is an instruction how to use this script from WSL terminal:
https://gist.github.com/dbalabka/20907893ba33f9ceebff3aaa8182f9c8
@dend how to prevent the previous toast notifications from being overwritten by new toast in action center? I want to keep all previous toasts and disappear only when I close it from action center
Adding a unique identifier for each notification.
Change the $Toast.Tag = "PowerShell"
to $Toast.Tag = [Guid]::NewGuid().ToString()
Maybe depends on your setup but simply sending it back to the other PowerShell version while in PS7 works (just need to wrap the code and start it using the older PS):
eg run from PS7 7.4.1 .".../testScript.ps1"
get-host
powershell
{
function Show-Notification
{
...
}
Show-Notification "test" "test"
get-host
}
get-host
For me, pwsh is PS7.4.1 and powershell is 5.1.22621.2506
So, it winds up executing as:
7.4.1
Shows notification and popup
5.1.22621.2506
7.4.1
When running as a Task using PS7 (aka pwsh), it works fine as well.
As long as the older PS version is there it should work. Once that disappears, this will probably fail to work. But, simple answer for the time being. Suppose too, idk, but if its possible to always install an old enough PS version then that might keep it working for even longer once the built-in version gets too high.
E: The example probably needs a slight adjustment depending on how its used. Its just an example that works for anyone that reads this and tries to use copy paste without understanding (I'm not putting in the whole noti code so add that). Also, some use cases may cause a script to not finish running and that needs adjustment for it to work.
@Xavron
Thanks for that. Somewhat bizarre that you need such an overhead just to show a Toast! But hey, a workaround anyway. Thanks.
Np. Between this and other silly things, I think my head is spinning :)P
(I know people in the linked issue thread seem to be feeling its not PS at fault... but ultimately its PS that has the problem so it is at fault. Kind of feels like Linux's xorg verses Wayland destruction that is still picking up the pieces after many years. Can't do anything about it but deal with workarounds. PS devs should code in another way but I get the impression they won't from its not their problem type of thinking that is prevalent among many devs. Its only legit to me if they wanted to cut out lots of uses.)
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)?