PowerShell – Call WMI Methods

Windows PowerShell can call WMI methods. As an example, on Windows Server 2003 and later there is a really cool WMI class named Win32_Volume. The reason I love this WMI class is because of the methods. The properties are easy to obtain by using the Get-WmiObject cmdlet. (One thing that is cool is the system property __Property_Count. This tells us there are 44 properties on the Win32_Volume WMI class.) This is seen here:

PS C:> Get-WmiObject -Class win32_volume

in spite of the fact that the Get-Member cmdlet clearly displays them as seen here:

PS C:> Get-WmiObject -Class win32_volume | Get-Member -MemberType method

you use the Get-WmiObject cmdlet to query for all instances of the Win32_Volume, and you use the pipeline to pass the objects to the Foreach-Object cmdlet. Inside the script block for the Foreach-Object cmdlet, you use the $_ automatic variable to refer to the current object on the pipeline. This allows you to call the DefragAnalysis method. This technique is seen here:

.

 

Posted on

Leave a Reply

Your email address will not be published. Required fields are marked *