Powershell: Listening to Windows RT events

Created on 4 Sep 2016  路  11Comments  路  Source: PowerShell/PowerShell

I tried using Powershell to listen to the Dismissed event of a ToastNotification as follows:

$toast = [Windows.UI.Notifications.ToastNotification]::new( ... )
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier( ... )
$notifier.Show($toast)

Register-ObjectEvent -InputObject $toast -EventName Dismissed -Action { ... }

Running this code, however, fails with the following exception message:
Windows PowerShell cannot subscribe to Windows RT events.

Therefore, I'd like to ask:

  • Does a workaround exist to subscribe to Windows RT events from Powershell?
  • Is there another way to query the current state or final result of a ToastNotification (i.e. whether it is Activated, Dismissed, or Failed) from Powershell?

Any help is much appreciated.

Issue-Question OS-Windows Up-for-Grabs WG-Engine

Most helpful comment

All 11 comments

We need to check with latest builds or simply close.

Any update on this in 2020? It's weird that I can do a ton of WinRT stuff, but then cannot register event handlers.

@citelao Do you test with latest PowerShell 7.0 build?

If you can get that working @iSazonov (or anyone out there) I'll send you a beer or something!

A lot of the APIs/References that uses have changed. I managed to compile and get it working on Windows 8 (maybe 8.1), but haven't been able to replicate on Windows 10 even after updating references etc. I even reached out to Dave at one point to see if he knew how to update it for Windows 10.

As for if it works on the current version, I test this in every Windows update and PowerShell release because I'm very invested in seeing this work. The answer today with Windows 10 1909 and PowerShell 7.1 is... no:

image

@Windos Please share you test script.

@iSazonov sure thing: https://gist.github.com/Windos/78ed980d926c634d0acede61e44a29af

Gist
Example of trying to register a Windows RT event via PowerShell, specifically here we're looking at events on Toast Notifications - Toast-RTEvent-Example.ps1

After reviewing this I see that PowerShell need a wrapper to process WinRT events.
I am not sure that it is stable area (API) to allow us to inject this in PowerShell engine. So best way to address the issue is to create a module in PowerShell Gallery.

Perhaps @rkeithhill could share thoughts.

@rjmholt @TylerLeonhardt Perhaps it is useful for PowerShell related projects.

It seems .Net Core has a code for WinRT events. https://github.com/dotnet/runtime/blob/c9edf7e621350ae8171e1ae4461d4a9e2c74c092/src/libraries/System.Runtime.WindowsRuntime/src/System/Runtime/InteropServices/WindowsRuntime/MarshalingHelpers.cs#L199 I don't know whether we use it.

GitHub
.NET is a cross-platform runtime for cloud, IoT, and desktop apps. - dotnet/runtime

I wish I could be of more help actually writing a solution to this, my C# is both rusty and rudimentary 鈽癸笍

I can point to where the check for WinRT events in the PowerShell code base is though, in case that helps anyone: https://github.com/PowerShell/PowerShell/blob/b7cb335f03fe2992d0cbd61699de9d9aafa1d7c1/src/System.Management.Automation/engine/EventManager.cs#L675

It is block condition only for writing user friendly message. Really AddEventHandler() below does not support WinRT events.
If you want to get a progress you could open an issue-question in .Net Runtime repo about how work with WinRT events in .Net Core.

Was this page helpful?
0 / 5 - 0 ratings