Summary: Learn how to find the key property of a WMI class by using a Windows PowerShell function.
Microsoft Scripting Guy Ed Wilson here. Well, I received a decent amount of feedback about version 1 of my HSGWMIhelper Windows PowerShell module. Today, I want to add a needed feature to the module: I need the ability to find a WMI key property from a WMI class. I added the Get-WMIKey function to the Scripting Guys Script Repository.
One thing I often need is to be able to do is find the key property of a WMI class. For example, after I have imported my HSGWMImoduleV2 module, I can use the Get-WmiKey function to retrieve the key property from the Win32_Process WMI class. The handle property is the key property from the class. This is shown here:
PS C:\> Import-Module hsg*v2
PS C:\> Get-WmiKey win32_process
I now start an instance of Notepad, and use the Get-Process cmdlet to retrieve information about the Notepad process as shown here:
PS C:\> notepad
PS C:\> Get-Process notepad
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
78 9 4272 8948 86 0.03 6872 notepad
I can then use the id property value (equals the handle property from WMI) with the [WMI] instance accelerator to retrieve that specific instance of the Win32_Process class. The following is the syntax for this command:
[wmi]"win32_process.handle=6872"
The command and associated output are shown in the following figure.
One thing that is great about the WMI instance accelerator is that instance methods are immediately available. This technique is shown in the following code:
([wmi]"win32_process.handle=6872").terminate()
The command and associated output are shown in the following figure.
In the Get-WmiKey function, the first thing I do is create help by using comment-based help (all the functions in my HSGWMIModuleV2 module have comment-based help). The help portion of the function is shown here.
function Get-WmiKey
{
<#
.Synopsis
This function returns the key property of a WMI class
.Description
This function returns the key property of a WMI class
.Example
Get-WMIKey win32_bios
Returns the key properties for the Win32_bios WMI class in root\ciimv2
.Example
Get-WmiKey -class Win32_product
Returns the key properties for the Win32_Product WMI class in root\cimv2
.Example
Get-WmiKey -class systemrestore -namespace root\default
Gets the key property from the systemrestore WMI class in the root\default
WMI namespace.
.Parameter Class
The name of the WMI class
.Parameter Namespace
The name of the WMI namespace. Defaults to root\cimv2
.Parameter Computer
The name of the computer. Defaults to local computer
.Notes
NAME: Get-WMIKey
AUTHOR: ed wilson, msft
LASTEDIT: 10/18/2011 17:38:20
KEYWORDS: Scripting Techniques, WMI
HSG: HSG-10-24-2011
.Link
Http://www.ScriptingGuys.com
#Requires -Version 2.0
#>
Next, I create the input parameters. I use parameter attributes to make the class parameter mandatory, and I assign it to the first position. The remainder of the parameters use techniques I have written about in the past.
Param(
[Parameter(Mandatory = $true,Position = 0)]
[string]$class,
[string]$namespace = "root\cimv2",
[string]$computer = $env:computername
)
Now, I do something pretty cool (I use this technique in other functions in the HSGWmiModuleV2 module). I cast the string in the $class variable to be an instance of a management object. I use parameter substitution to put together a complete path: computer name, WMI namespace, and WMI class name:
[wmiclass]$class = "\\{0}\{1}:{2}" -f $computer,$namespace,$class
Now I obtain a collection of the properties of the class, and I expand the qualifiers associated with each property. I then look for the name that is equal to “key” and I print out the property names. It is important to note that some WMI classes have multiple keys, such as Win32_product. This portion of the function is shown here:
$class.Properties |
Select-object @{Name="PropertyName";Expression={$_.name}} `
-ExpandProperty Qualifiers |
Where-object {$_.Name -eq "key"} |
ForEach-Object {$_.PropertyName}
} #end GetWmiKey
I included the Get-WMiKey function in the second version of my HSGWmiModuleV2 module. You will find the complete module on the Scripting Guys Script Repository.
That’s it for now. See you tomorrow when I will add another really cool WMI function to my WMI module.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy