Use a PowerShell Function to Get WMI Key Property Values

Use a PowerShell Function to Get WMI Key Property Values

  • Comments 1
  • Likes

Summary: Microsoft Scripting Guy Ed Wilson discusses his version 3 of a Windows PowerShell WMI helper module.

 

Microsoft Scripting Guy Ed Wilson here. I have really enjoyed working on my HSGWMIModule project this week. On Monday, I created the base WMI module, and included a couple of really great functions that query the WMI schema and return WMI class methods and properties. On Tuesday, I added a function that returns the key property of a WMI class. This is useful when working WMI instance methods. Today, I create a function that will return the values of those key properties. There are times when I only need to know the key property of a WMI class, and there are other times when I want to see the path to all the possible instances of a WMI class. Today’s function addresses these situations.

Note   As I did in the two previous Hey, Scripting Guy! Blog posts, I have uploaded the current iteration, version 3, of the WMI module to the Scripting Guys Script Repository.

To use HSGWMImoduleV3, I first must “install” it. Installation is simple; I use my Copy-Modules function from the Windows PowerShell ISE Profile and Modules project I wrote. I discussed this on Friday, October 21, 2011.

Note   If you need more information about Windows PowerShell modules, see this collection of Hey, Scripting Guy! posts.

After you have installed the module, import the module by using the Import-Module cmdlet. It is not necessary to use the complete module name; all that is required is to use wildcard characters that uniquely identify the module name. I use the command seen here to load the module into my Windows PowerShell ISE:

Import-Module hsg*wmi*3

After it's loaded (imported), all the functions contained in the module load onto the function drive. As shown in the following figure, I use the Get-WMIKeyvalue function to find the path to instances of the Win32_Process WMI class.

Image of using Get-WMIKeyvalue function

Most of the lines in the Get-WMIKeyValue function are comment-based help. Therefore, when I use the Get-Help cmdlet, as shown in the following figure, nicely formatted help is displayed in the output pane.

Image of using Get-Help cmdlet

The main portion of the Get-WMIKeyValue function consists of three parameters, a Get-WMIObject command, and a Select-Object command:

Param(

    [Parameter(Mandatory=$true)]

    [string]$class,

    [string]$computername = $env:COMPUTERNAME,

    [string]$namespace = "root\cimv2"

)

  Get-WmiObject -Class $class -ComputerName $computername -Namespace $namespace |

  Select __PATH

} #end function get-WmiKeyvalue

 

I call the Get-WMIKeyValue function by passing at least the WMI class name. The Get-WmiObject cmdlet retrieves the class, and pipes the management object to the Select-Object cmdlet where I choose the __Path system property. The __Path system property returns to the caller of the function.

Suppose I want to use the Invoke-WMIMethod cmdlet to terminate an instance of Notepad.exe. I use the Get-WMiKeyValue function to return instances of the Win32_Process WMI class. This command is shown here:

Get-WmiKeyvalue win32_process

I use the Get-Process cmdlet to ensure I get the right instance. The value in the ID property returned by Get-Process matches the value in the handle WMI property of the Win32_Process class. I copy the path, and paste it into the path property of the Invoke-WMIMethod class. I then call the terminate method. The Invoke-WMIMethod command is shown here:

Invoke-WmiMethod -Path '\\MRED\root\cimv2:Win32_Process.Handle="4652"' -name terminate

The commands and associated output are shown in the following figure.

Image of commands and associated output

 

Well, that is about all there is to the Get-WmiKeyValue function. Download version 3 of the WMI helper function module from the Scripting Guys Script Repository, and let me know what you think. If there are things you would like to see, just let me know. See you tomorrow.

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

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    .... Hmmmmmmmmm ... !?

    And I wonder why ... why is the function not called "Get-WmiPathValue" ???

    I can't find any "Key" property in this article!

    Klaus.