I have been creating a powershell script to display toast notifications, this code works but there is one method on the toastnotification object I dont understand how to use:

$Load = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$Load = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($ToastXml)

Looking at the [Windows.UI.Notifications.ToastNotificationManager] object there is one method named "GetForUser()" https://learn.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotificationmanager.getforuser?view=winrt-19041

This method needs a Windows.System.User object as input. https://learn.microsoft.com/en-us/uwp/api/windows.system.user?view=winrt-19041

I have tried the following code

$Load = [Windows.System.User, Windows.System, ContentType = WindowsRuntime]
$users = [Windows.System.User]::FindAllAsync()

$users is then a "System.__ComObject" without any methods.

So the question is, how can i get a Windows.System.User in PowerShell that I can use with the GetForUser() method of Windows.UI.Notifications.ToastNotificationManager?

I have also tried managed code

$code = @"
using Windows.System;
namespace CUser
{
    public static class GetUsers{
        public static void Main(){
                IReadOnlyList<User> users = await User.FindAllAsync(UserType.LocalUser, UserAuthenticationStatus.LocallyAuthenticated);
                User user = users.FirstOrDefault();
        }
    }
    
}
"@
Add-Type -TypeDefinition $code -Language CSharp 

But that gives the error: "The type or namespace name 'System' does not exist in the namespace 'Windows' (are you missing an assembly reference?)"

I am not sure what assembly or dll contains the "Windows.System" reference.

1

There are 1 best solutions below

0
On

I was searching for a similar problem with DeviceInformation and ran across your question. The solution turned out to be in this blog post https://fleexlab.blogspot.com/2018/02/using-winrts-iasyncoperation-in.html

Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
 $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
 $netTask = $asTask.Invoke($null, @($WinRtTask))
 $netTask.Wait(-1) | Out-Null
 $netTask.Result
}

You can then run FindAllAsync() like this

$windowsSystemUserClass = [Windows.System.User, Windows.System, ContentType = WindowsRuntime]
$users = Await ([Windows.System.User]::FindAllAsync()) ([System.Collections.Generic.IReadOnlyList`1[Windows.System.User]])