Introduction to PowerShell’s WMI Techniques
One of the most useful jobs for PowerShell is to create a bank of WMI based scripts. Furthermore, scripting WMI with PowerShell is much easier and more efficient than WMI with VBScript.
Topics for PowerShell WMI Techniques
- Get-WmiObject
- WmiObject Get-Member
- WmiObject – Help with Parameters
- WmiObject $variables
- [WMI] Type
- New Features PowerShell v 3.0
- PowerShell to Check Computer Uptime
- Summary of WMI and PowerShell
♣
WMI Perspective
To appreciate the beauty of a crystal, you should examine the facets from different angles. The same applies to the diamond that is WMI; I recommend that to gain perspective we investigate WMI from these five angles.
- Imagine WMI as a database, which keeps information about a computer’s components such as the: BIOS, services and network settings.
- Regard WMI as a method for collecting data about a machine’s hardware and software.
- View WMI as a pipe, which magically connects to the core of any Microsoft operating system (post 2000).
- Think of WMI as a having its own PowerShell dialect, for example the WQL select clause.
- Treat WMI as a microscope, and use it to probe the operating system’s objects and their properties.
Whenever I think about WMI for any length of time, it hits me: the operating system must know everything that’s going on! Therefore, provided the PowerShell script has sufficient rights, it can use WMI and tap into that vast fountain of operating system knowledge. The Windows operating system must know ‘stuff’ such as how much memory each process is using, how much free space there is on each partition, which devices are on which Bus. It is even possible to manipulate or ‘set’ values on some of these properties and thus achieve configuration via scripts rather than GUIs.
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s 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. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Get-WmiObject – Getting Started
Get-WmiObject is the key command. As we will see, it unlocks zillions of classes such as Win32_Computer, here is the simple example that got me interested in PowerShell WMI:
# PowerShell GetWmiObject Example
Get-WmiObject Win32_Computersystem
The result was:
Domain : cp.mosel
Manufacturer : VIAK8T
Model : AWRDACPI
Name : BIG-SERVER
PrimaryOwnerName : Guy
TotalPhysicalMemory : 2146910208
I was curious to discover what other WMI Objects were available for scripting; then I remembered the -List switch from another PowerShell command (Get-Eventlog -List). Thus I tried:
# PowerShell Get-WmiObject example to list classes
Clear-Host
Get-WmiObject -List
Next, I redirected the output from the screen to a file by appending ‘Out-File’:
out-File WmiObject.txt. To make:
# PowerShell Get-WmiObject example
Clear-Host
Get-WmiObject -List | Out-File WmiObject.txt
See a Review of SolarWinds WMI Monitor »
Researching WMI Classes with ‘Where-Object’
My next problem was the list was too long, therefore I added a ‘Where’ filter
Get-WmiObject -List | Where-Object {$_.name -Match "Win32"}
And even better:
Get-WmiObject -List |Where-Object {$_.name -Match "Win32"} `
| Out-File D:\wmi\win.txt
Learning Points
Note 1: The tiny backtick (`) tells PowerShell that the command continues on the next line.
Note 2: On other pages I use plain ‘Where’, or even ‘?’ instead of the full ‘Where-Object’.
Note 3: I expect you have guessed that PowerShell commands are case insensitive. At present I am not sure which is best, WmiObject, wmiObject or WmiObject – they all produce the same results. Another minor point, since the verb ‘get’ is the default, you can shorten the command to:
WmiObject Win32_computersystem. Or if you like aliases: gwmi Win32_computersystem.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
WmiObject Get-Member
Wouldn’t it be useful to get a list of all the properties on a PowerShell object? The answer is Get-Member. Here is an example of applying the Get-Member command to examine the Win32_Logical Disk:
Example 1
# List Get-WmiObject properties
Clear-Host
Get-WmiObject Win32_LogicalDisk | Get-Member
Sample Output (Heavily truncated)
Get-WmiObject Win32_LogicalDisk | Get-Member
Example 2
Here is another example, this time PowerShell interrogates the LogicalDisk:
TypeName: System.Management.ManagementObject#root\cimv2\Win32_LogicalDisk
Name MemberType Definition
—– ———– ———-
add_Disposed Method System.Void add_Disposed(EventHandler value)
Clone Method System.Object Clone()
…………..
Access Property System.UInt16 Access {get;}
Availability Property System.UInt16 Availability {get;}
BlockSize Property System.UInt64 BlockSize {get;}
Caption Property System.String Caption {get;}
Compressed Property System.Boolean Compressed {get;}
…………..
WmiObject – Help with Parameters
When I am in ‘let us get started’ mode, I gloss over the optional PowerShell commands. However, when we need to troubleshoot, then the secret of success is knowledge of a command’s parameters. To reveal the full list of parameter, let’s call for help:
# PowerShell Get-WmiObject see more parameters
Clear-Host
Get-Help Get-Wmiobject -full
Five useful Wmiobject Parameters
®