There is one last WMI accelerator to look at – [WMI] – which is an accelerator for System.Management.ManagementObject. This is the same object type that Get-WMIObject returns. [WMI] gets an object representing an existing WMI object
If we start with Get-WmiObject
$w1 = Get-WmiObject -Class Win32_Process -Filter "Name = ‘notepad.exe’"
$w1 | get-member
If we try to emulate this with [WMI] we start to run into some issues. The only path that I could get to work was
$w2 = [WMI]’rootcimv2:Win32_Process.Handle="4112"’
$w2 | get-member
I tried name, description and other candidates but none seemed to work. Only thing I can think of is that to create that PowerShell is constraining the object creation.
Alternatively the object could be created like this
$y = [WMI]""
$y
$y.psbase.Path = ‘\PCRS2rootcimv2:Win32_Process.Handle="4112"’
Comparing the results of these three techniques we get
Compare-Object -ReferenceObject $w1 -DifferenceObject $w2
Compare-Object -ReferenceObject $w1 -DifferenceObject $y
Both of which indicate the objects are the same.
Looking at creating this with .NET
$z = New-Object -TypeName System.Management.ManagementObject -ArgumentList ‘\.rootcimv2:Win32_Process.Handle="4112"’
$z | Get-Member
Compare-Object -ReferenceObject $w1 -DifferenceObject $z
Again the same object is created and I could only get it to build when I use the handle.
The object creation works when I do
$t = Get-WmiObject -Class Win32_process -Filter ‘name="notepad.exe"’
As we need to know the path before we can use [WMI] it may be easier to use get-WmiObject instead. I’d be interested in hearing other peoples take on [WMI] and how they are using it
Share this post : |
Hi Richard,
Great blog!
Just want to share my experiences with [WMI]. As you point out, you need to know the WMI path of the object before being able to use [WMI]. I use it a lot when writing script for Hyper-V. When you create a virtual machine or virtual network in Hyper-V using script, the creation method usually return the full WMI path of the newly created object as text string.
To access the new object, I simply cast this string to [WMI] instead of using get-wmiobject and filter the output with the WMI path string.
I refer you to my blog http://dungkHoang.spaces.live.com to see examples of using [WMI]
Hope that helps
/Dung
Hi
Thanks for the link. I don’t think [WMI] is used that much and the more examples we can show the better
Hi Richard, I recommend checking out the WMI Object Identifiers and Keys post by Jeffrey Snover which explains why only certain properties work as an identifier when using the [WMI] accellerator.
In the case of Win32_Process, the only Key property is Handle.
Regards,
Arnoud
Thank you. I’d forgotten about that