Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Get-CIM_WMI_PropertyValueIDName
- {
- <#
- CBH goes here
- .NOTES
- original idea = u/SeeminglyScience
- post = SeeminglyScience comments on is there a builtin enum for "PCSystemType"?
- Post URL = https://www.reddit.com/r/PowerShell/comments/8jdczz/is_there_a_builtin_enum_for_pcsystemtype/dyzw5fq/
- #>
- [CmdletBinding ()]
- Param (
- [Parameter (
- Position = 0,
- Mandatory)]
- [string]
- $ClassName,
- [Parameter (
- Position = 1,
- Mandatory)]
- [string]
- $PropertyName,
- [Parameter (
- Position = 2,
- Mandatory)]
- [int]
- $PropertyValueID
- )
- $ClassProperties = Get-CimInstance -ClassName $ClassName -ErrorAction Ignore
- # if the $ClassName or $PropertyName are invlaid, return $Null
- if (([string]::IsNullOrEmpty($ClassProperties)) -or
- ($ClassProperties.PSObject.Properties.Name -notcontains $PropertyName))
- {
- return $Null
- }
- # the CIM_* classes seem to lack many of the LocalizedQualifiers
- # that means the CreationClass appears to be required
- # the usual result is CIM_* becomes Win32_*
- $CreationClassName = $ClassProperties.CreationClassName
- $Session = [CimSession]::Create('LocalHost')
- # Add operation options that retrieve localized values for mappings
- $OperationOptions = [Microsoft.Management.Infrastructure.Options.CimOperationOptions]@{
- Flags = [Microsoft.Management.Infrastructure.Options.CimOperationFlags]::LocalizedQualifiers
- }
- $PropertyInfo = $Session.
- GetClass('ROOT/CIMV2', $CreationClassName, $OperationOptions).
- CimClassProperties[$PropertyName]
- # finally found a PropertyID that is NOT a direct index into Values
- # it's Win32_OperatingSystem ProductType
- # the ValueMap = 1,2,3
- # the Values = 'Work Station', 'Domain Controller', 'Server'
- $ValueMap = $PropertyInfo.Qualifiers['ValueMap'].Value
- $Values = $PropertyInfo.Qualifiers['Values'].Value
- # check to see if there is a ValueMap
- if (($ValueMap) -and
- ($ValueMap -contains "$PropertyValueID"))
- {
- # if yes, the ID needs to be a numeric string, not an INT
- $Values[$ValueMap.IndexOf("$PropertyValueID")]
- }
- # check for out-of-bounds index
- elseif ($PropertyValueID -in 0..$Values.GetUpperBound(0))
- {
- $Values[$PropertyValueID]
- }
- else
- {
- return $Null
- }
- $Session.Dispose()
- } # end >> function Get-CIM_WMI_PropertyValueIDName
- # this should return "Desktop"
- Get-CIM_WMI_PropertyValueIDName -ClassName Win32_ComputerSystem -PropertyName PCSystemType -PropertyValueID 1
- # these otta return $Null
- Get-CIM_WMI_PropertyValueIDName -ClassName Win32_ComputerSystem -PropertyName PCSystemType -PropertyValueID 666
- Get-CIM_WMI_PropertyValueIDName -ClassName CIM_ComputerSystem -PropertyName FAKE_PropName -PropertyValueID 1
- Get-CIM_WMI_PropertyValueIDName -ClassName FAKE_ClassName -PropertyName WakeUpType -PropertyValueID 1
- # this one otta return "Work Station"
- Get-CIM_WMI_PropertyValueIDName CIM_OperatingSystem ProductType 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement