Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell [WMI] Type

Introduction to PowerShell's [WMI] Type

The [WMI] Type technique is not for the beginner, for novices I suggest they concentrate on Get-WmiObject.  Even for experienced PowerShell script writers I question the usefulness of the [WMI] Type construction.  While it looks flashy, the syntax is picky, therefore I am not sure that it is better than the straightforward Get-WmiObject.

Topics for PowerShell [WMI] Type

 ♣

[WMI] Type - Simple Example

This example employs the called Win32_ComputerSystem.  By introducing variables I highlight the WMI particular class, and also remind you to change the name of $Srv.  Incidentally, neither LocalHost nor '.' works for as a value for your computer name, see what I mean about [WMI] Type being picky?

# [WMI] Type example
Clear-Host
$Srv = "Longhorn"
$CimPlace = "Root\Cimv2:Win32_ComputerSystem"
[WMI]$LowDown = "$CimPlace.name='$Srv'"
$LowDown

Note 1: The reason that the above example works is that luckily I have used the .name property, which is the correct primary key for the WMI class Win32_ComputerSystem.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

[WMI] Type Get-Member

As with other PowerShell cmdlets, once you have created the [WMI] Type object you can research its properties with Get-Member:

# PowerShell [WMI] Type Properties
Clear-Host
$Srv = "Longhorn"
[WMI]$LowDown = "Root\Cimv2:Win32_ComputerSystem.name='$Srv'"
$LowDown | Get-Member -memberType Property [a-z]*

Note 2: With this example I have defined [WMI]$LowDown more directly.

Alternative Method [WMI] Root..

# PowerShell [WMI] Type Properties
Clear-Host
$Srv = "Longhorn"
$Stuff = [WMI]"Root\Cimv2:Win32_ComputerSystem.name='$Srv'"
$Stuff | Get-Member -memberType Property [a-z]*

Note 3: This simply alters the position of the [WMI] Type  from
[WMI]$LowDown = "Root\...
To
$Stuff = [WMI]"Root\..

 

Researching the Primary Key for a WMI Class

Here is an example which creates a function which extracts the primary key for any named or given WMI class.

# Function to Discover WMI Primary Keys
Clear-Host
Function Get-WmiKey
{
$Class = [WmiClass]$args[0]
$Class.Properties | `
Select @{Name="PName";Expression={$_.name}} -Expand Qualifiers | `
Where {$_.Name -eq "key"} | `
foreach {$_.Pname}
}

Get-WmiKey Win32_LogicalDisk

Note 4:  The whole point of creating this function is that you can substitute any WMI class for Win32_LogicalDisk.

Note 5: Alternatively you can research WMI Classes with: WBEMTest.

Note 6:  See more on PowerShell functions.

[WMI] Type Picky Syntax  (Or Silly Me?)

Clear-Host
$Hardy = ""
[WMI]$Hardy="Root\CimV2:Win32_LogicalDisk.DeviceID='C:'"
$Hardy

Note 6: This example drove me mad.  The reason was that I introduced white space in an attempt to make the command more readable.  Introducing a space either side of the equals sign produced the "Invalid parameter " error.  Wrong, silly me.

.DeviceID = 'C:'" Just does not work.  

Neither did introducing a space between the two sets of speech marks:
.DeviceID='C:'  "   - Wrong.

Summary of PowerShell [WMI] Type

PowerShell's [WMI] Type technique is not for the beginner.  Even for experienced PowerShell script writers I question the usefulness of the [WMI] Type construction.  While it looks flashy, the syntax is picky, and I am not sure that it is better than the straightforward Get-WmiObject.

If you like this page then please share it with your friends

 


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • PowerShell -Query   • PowerShell Select

Win32_pingstatus   • WMI Win32_NetworkAdapter   • Win32_NetworkAdapterConfig

Disable NIC   • PowerShell -Filter   • Windows PowerShell   • PowerShell 3.0 Network

Please email me if you have any example scripts. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.