Created
August 27, 2021 03:07
-
-
Save SteveL-MSFT/390cfd4b7900c2bc587d076364474a54 to your computer and use it in GitHub Desktop.
Example calling WnRT API from PS7 to get Windows version information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adapted from https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/identifying-windows-version-part-2 | |
# download and unzip latest https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref 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', | |
'OSVersionFull', | |
'FlightRing', | |
'App', | |
'AppVer' | |
$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