In the previous tip we showed that the new Get-CimInstance command is missing the important “__Path” property that was returned by Get-WmiObject. Let’s add this property back to Get-CimInstance.

Every instance returned by Get-CimInstance is of type [Microsoft.Management.Infrastructure.CimInstance], so Update-TypeData can be used to add a new property to this type:

$code = {
 # get key properties
 $keys = $this.psbase.CimClass.CimClassProperties.Where{$_.Qualifiers.Name -eq 'Key'}.Name
 $pairs = foreach($key in $keys)
 {
    '{0}="{1}"' -f $key, $this.$key
 }
 # add server name
 $path = '\\{0}\{1}:{2}.{3}' -f $this.CimSystemProperties.ServerName.ToUpper(),
 # add namespace
  $this.CimSystemProperties.Namespace.Replace("/","\"),
  # add class
  $this.CimSystemProperties.ClassName,
  # add key properties
  ($pairs -join ',')

  return $path
}

Update-TypeData -TypeName Microsoft.Management.Infrastructure.CimInstance -MemberType ScriptProperty -MemberName __Path -Value $code -Force

Once you run this code, all of your CIM instances have a __Path property again. It differs slightly because the “new” __Path property quotes all key values. For any use cases we tested, this does not make a difference:

 
PS> $old = Get-WmiObject -Class Win32_BIOS

PS> $new = Get-CimInstance -ClassName Win32_BIOS


PS> $old.__PATH
\\DESKTOP-8DVNI43\root\cimv2:Win32_BIOS.Name="1.0.13",SoftwareElementID="1.0.13",SoftwareElementState=3,TargetOperatingSystem=0,Version="DELL   - 20170001"

PS> $new.__Path
\\DESKTOP-8DVNI43\root\cimv2:Win32_BIOS.Name="1.0.13",SoftwareElementID="1.0.13",SoftwareElementState="3",TargetOperatingSystem="0",Version="DELL   - 20170001"


PS> [wmi]($old.__Path)

SMBIOSBIOSVersion : 1.0.13
Manufacturer      : Dell Inc.
Name              : 1.0.13
SerialNumber      : 4ZKM0Z2
Version           : DELL   - 20170001

PS> [wmi]($new.__Path)

SMBIOSBIOSVersion : 1.0.13
Manufacturer      : Dell Inc.
Name              : 1.0.13
SerialNumber      : 4ZKM0Z2
Version           : DELL   - 20170001  
 


Twitter This Tip! ReTweet this Tip!

Anonymous