Last active
April 21, 2024 11:39
-
-
Save pronichkin/dc150c79212831a4049d29019d05fabe to your computer and use it in GitHub Desktop.
This file contains 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
Set-StrictMode -Version 'Latest' | |
# Collection of attributes to extract | |
$attributeName = [System.Collections.Generic.List[System.String]]::new() | |
$attributeName.Add( 'DeviceFamily' ) | |
$attributeName.Add( 'OSVersionFull' ) | |
$attributeName.Add( 'FlightRing' ) | |
$attributeName.Add( 'App' ) | |
$attributeName.Add( 'AppVer' ) | |
Function | |
Get-wrtTaskResult | |
{ | |
<# | |
.Synopsis | |
Wait for Windows Runtime task to complete and return result | |
.Notes | |
Inspired by https://superuser.com/questions/1341997/using-a-uwp-api-namespace-in-powershell | |
Expanded for readability | |
#> | |
[System.Management.Automation.CmdletBindingAttribute()] | |
[System.Management.Automation.OutputTypeAttribute( | |
[System.Object] | |
)] | |
Param | |
( | |
[System.Management.Automation.ParameterAttribute( | |
Mandatory = $True, | |
ValueFromPipeline = $True | |
)] | |
[System.Management.Automation.ValidateNotNullOrEmptyAttribute()] | |
[System.Management.Automation.AliasAttribute( | |
'Task' | |
)] | |
[System.__ComObject] | |
$InputObject | |
, | |
[System.Management.Automation.ParameterAttribute( | |
Mandatory = $True | |
)] | |
[System.Management.Automation.ValidateNotNullOrEmptyAttribute()] | |
[System.Management.Automation.AliasAttribute( | |
'Type' | |
)] | |
[System.Type] | |
$ResultType | |
) | |
Begin | |
{ | |
[System.Void][System.Reflection.Assembly]::LoadWithPartialName( 'System.Runtime.WindowsRuntime' ) | |
$MethodDefinition = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object -FilterScript { | |
$psItem.Name -eq 'AsTask' -and | |
$psItem.GetParameters().Count -eq 1 -and | |
$psItem.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' | |
} | |
# Convert from Windows.Foundation.IAsyncOperation to System.Threading.Tasks.Task | |
$Method = $MethodDefinition.MakeGenericMethod( $ResultType ) | |
} | |
Process | |
{ | |
$Argument = @( | |
$Null # Object | |
$InputObject # Parameter | |
) | |
$Task = $Method.Invoke.Invoke( $Argument ) | |
[System.Void]( $Task.Wait( -1 ) ) | |
Return $Task.Result | |
} | |
End | |
{} | |
} | |
Switch | |
( | |
$psVersionTable.psEdition | |
) | |
{ | |
'Core' | |
{ | |
throw 'WinRT APIs do not work in PowerShell (Core)' | |
} | |
'Desktop' | |
{ | |
# Load WinRT type | |
[System.Void][Windows.System.Profile.AnalyticsInfo,Windows.System.Profile,ContentType=WindowsRuntime] | |
# Type of data to return in this scenario | |
$Type = [System.Collections.Generic.IReadOnlyDictionary[System.String,System.String]] | |
# Create task | |
$Task = [Windows.System.Profile.AnalyticsInfo]::GetSystemPropertiesAsync( $attributeName ) | |
# Extract task result | |
$Task | Get-wrtTaskResult -Type $Type | |
} | |
Default | |
{ | |
throw 'Unknown PowerShell edition' | |
} | |
} | |
<# | |
Sample output | |
Key Value | |
--- ----- | |
OSVersionFull 10.0.19043.1023.amd64fre.vb_release.191206-1406 | |
FlightRing External | |
App PowerShell_ISE.exe | |
AppVer 10.0.19041.1 | |
DeviceFamily Windows.Desktop | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment