Thursday, August 06, 2009

Static vs Dynamic WMI Methods with PowerShell

In yesterday’s  PowerShell and .NET Framework – Similarities and Differences! article, I wrote about how there was stuff you have to know in order to use PowerShell efficiently. In a recent Hey, Scripting Guy! Blog article, The Scripting guys talk about the differences between a static method on a class and an instance-based method. They use the WMI Win32_Process class as an example. In this WMI class, there is an instance based method, Terminate, that lets you terminate a specific process. As the Scripting Guys point out, there’s no Create method. That’s because the  Create method is a static method which you invoke a slightly different way.

To create a process, you’d do the following:

PSH [C:\foo]: ([wmiclass]"Win32_Process").Create("NotePad")

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 2
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ProcessId        : 8828
ReturnValue      : 0

This is yet another one of those things you need to know about using WMI with PowerShell: how to access static vs. instance based methods.

Technorati Tags: ,,

No comments: