Advertisement
Lee_Dailey

function Get-CIM_WMI_PropertyValueIDName [2018.06.04]

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