Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example calling WnRT API from PS7 to get Windows version information
# adapted from
# download and unzip latest and get these dlls from the 'lib' folder
Add-Type -AssemblyName ./winrt.runtime.dll
Add-Type -AssemblyName .\Microsoft.Windows.SDK.NET.dll
# define call and information to query
[Collections.Generic.List[System.String]]$names = 'DeviceFamily',
$task = [Windows.System.Profile.AnalyticsInfo]::GetSystemPropertiesAsync($names)
# use reflection to find method definition
$definition = [System.WindowsRuntimeSystemExtensions].GetMethods().Where{
$_.Name -eq 'AsTask' -and
$_.GetParameters().Count -eq 1 -and
$_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1'
# create generic method
$Method = $definition.MakeGenericMethod( [System.Collections.Generic.IReadOnlyDictionary[System.String,System.String]] )
# call async method and wait for completion
$task = $Method.Invoke.Invoke($null, $task)
$null = $task.Wait(-1)
# emit output
$output = $task.Result
($output.AdditionalTypeData.GetEnumerator() | Select-Object -First 1).value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment