Advertisement
Lee_Dailey

function Get-CIM_WMI_PropertyValueIDName

May 24th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-CIM_WMI_PropertyValueIDName
  2.     {
  3.     <#
  4.     CBH goes here
  5.  
  6.     .NOTES
  7.         original idea = u/SeeminglyScience
  8.         post = SeeminglyScience comments on is there a builtin enum for "PCSystemType"?
  9.         Post URL = https://www.reddit.com/r/PowerShell/comments/8jdczz/is_there_a_builtin_enum_for_pcsystemtype/dyzw5fq/
  10.     #>
  11.  
  12.     [CmdletBinding ()]
  13.     Param (
  14.         [Parameter (
  15.             Position = 0,
  16.             Mandatory)]
  17.             [string]
  18.             $ClassName,
  19.  
  20.         [Parameter (
  21.             Position = 1,
  22.             Mandatory)]
  23.             [string]
  24.             $PropertyName,
  25.  
  26.         [Parameter (
  27.             Position = 2,
  28.             Mandatory)]
  29.             [int]
  30.             $PropertyValueID
  31.         )
  32.  
  33.     $ClassProperties = Get-CimInstance -ClassName $ClassName -ErrorAction Ignore
  34.     # if the $ClassName or $PropertyName are invlaid, return $Null
  35.     if (([string]::IsNullOrEmpty($ClassProperties)) -or
  36.         ($ClassProperties.PSObject.Properties.Name -notcontains $PropertyName))
  37.         {
  38.         return $Null
  39.         }
  40.  
  41.     # the CIM_* classes seem to lack many of the LocalizedQualifiers
  42.     #    that means the CreationClass appears to be required
  43.     #    the usual result is CIM_* becomes Win32_*
  44.     $CreationClassName = $ClassProperties.CreationClassName
  45.  
  46.     $Session = [CimSession]::Create('LocalHost')
  47.  
  48.     # Add operation options that retrieve localized values for mappings
  49.     $OperationOptions = [Microsoft.Management.Infrastructure.Options.CimOperationOptions]@{
  50.         Flags = [Microsoft.Management.Infrastructure.Options.CimOperationFlags]::LocalizedQualifiers
  51.         }
  52.  
  53.     $PropertyInfo = $Session.
  54.         GetClass('ROOT/CIMV2', $CreationClassName, $OperationOptions).
  55.         CimClassProperties[$PropertyName]
  56.  
  57.     # finally found a PropertyID that is NOT a direct index into Values
  58.     #    it's Win32_OperatingSystem ProductType
  59.     #    the ValueMap = 1,2,3
  60.     #    the Values = 'Work Station', 'Domain Controller', 'Server'
  61.     $ValueMap = $PropertyInfo.Qualifiers['ValueMap'].Value
  62.     $Values = $PropertyInfo.Qualifiers['Values'].Value
  63.  
  64.     # check to see if there is a ValueMap
  65.     if (($ValueMap) -and
  66.         ($ValueMap -contains "$PropertyValueID"))
  67.         {
  68.         # if yes, the ID needs to be a numeric string, not an INT
  69.         $Values[$ValueMap.IndexOf("$PropertyValueID")]
  70.         }
  71.         # check for out-of-bounds index
  72.         elseif ($PropertyValueID -in 0..$Values.GetUpperBound(0))
  73.         {
  74.         $Values[$PropertyValueID]
  75.         }
  76.         else
  77.         {
  78.         return $Null
  79.         }
  80.  
  81.     $Session.Dispose()
  82.  
  83.     } # end >> function Get-CIM_WMI_PropertyValueIDName
  84.  
  85.  
  86. # this should return "Desktop"
  87. Get-CIM_WMI_PropertyValueIDName -ClassName Win32_ComputerSystem -PropertyName PCSystemType -PropertyValueID 1
  88.  
  89. # these otta return $Null
  90. Get-CIM_WMI_PropertyValueIDName -ClassName Win32_ComputerSystem -PropertyName PCSystemType -PropertyValueID 666
  91. Get-CIM_WMI_PropertyValueIDName -ClassName CIM_ComputerSystem -PropertyName FAKE_PropName -PropertyValueID 1
  92. Get-CIM_WMI_PropertyValueIDName -ClassName FAKE_ClassName -PropertyName WakeUpType -PropertyValueID 1
  93.  
  94. # this one otta return "Work Station"
  95. Get-CIM_WMI_PropertyValueIDName CIM_OperatingSystem ProductType 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement