PowerShell Scripting Basics: WMI Techniques

PowerShell Scripting Basics_ WMI & gwmi Techniques

PowerShell’s File Techniques Applied toWin32_Computersystem

Our mission on this page is to combine opening files with WMI techniques.  Specifically, to open a text file, read the hostnames (machine names), then apply that name to a PowerShell Win32_Computersystem script.

Topics for WMI File Techniques

Preparing Get-WmiObject (gwmi) with PowerShell’s Get-Help

Before we use PowerShell’s Win32_Computersystem, let us investigate the master cmdlet Get-WmiObject.  In particular, we need to understand the syntax of parameters such as -Class and -ComputerName.  Incidentally, you can use the alias gwmi instead of Get-WmiObject.


# Help with PowerShell WMI object:
Get-Help Get-WmiObject -full

Note 1:  Plain: Get-WmiObject -List returns so many classes we need to refine our search with a where filter.


# Check WmiObject Classes
Clear-Host
$Type = "Computer"
Get-WmiObject -List | Where-Object {$_.name -Match $Type}

Note 2:  The above script is setup so that you can easily edit $Type, for example, “Win32_Computer”.

WMI Class Win32_Computersystem

The heart of our main script is Win32_Computersystem


 Get-WmiObject Win32_Computersystem

Other variations of Get-WmiObject that you could try are:

Get-WmiObject Win32_Bios
Get-WmiObject Win32_LogicalDisk
Get-WmiObject Win32_MemoryDevice

Guy Recommends: Free WMI Monitor for PowerShell (FREE TOOL)Solarwinds 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 for PowerShell so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guesswork 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.

SolarWinds WMI Monitor Download 100% Free Tool

Research More Properties of Win32_Computersystem with Get-Member

Get-Member is a trusty technique to research PowerShell cmdlet properties and methods.  We can filter the output with the -MemberType parameter thus:


# Properties for PowerShell Win32_ComputerSystem object:
Clear-Host
Get-WmiObject Win32_Computersystem | Get-Member -Membertype property [a-z]*

Note 3:  Gwmi is an alias for Get-WmiObject, handy for shortening scripts.

 Win32_Computersystem Example 1


# Example of PowerShell ComputerSystem object:
Clear-Host
Get-WmiObject Win32_Computersystem `
| Format-List Name, manufacturer, model, SystemType

 Win32_Computersystem Example 2


 PowerShell WMI Computer object:
Clear-Host
Get-WmiObject Win32_Computersystem `
|  Format-List Name, TotalPhysicalMemory, NumberOfLogicalProcessors

List of Win32_ComputerSystem Properties

Name
—-
AdminPasswordStatus
AutomaticManagedPagefile
AutomaticResetBootOption
AutomaticResetCapability
BootOptionOnLimit
BootOptionOnWatchDog
BootROMSupported
BootupState
Caption
ChassisBootupState
CreationClassName
CurrentTimeZone
DaylightInEffect
Description
DNSHostName
Domain
DomainRole
EnableDaylightSavingsTime
FrontPanelResetStatus
InfraredSupported
InitialLoadInfo
InstallDate
KeyboardPasswordStatus
LastLoadInfo
Manufacturer
Model
Name
NameFormat
NetworkServerModeEnabled
NumberOfLogicalProcessors
NumberOfProcessors
OEMLogoBitmap
OEMStringArray
PartOfDomain
PauseAfterReset
PCSystemType
PowerManagementCapabilities
PowerManagementSupported
PowerOnPasswordStatus
PowerState
PowerSupplyState
PrimaryOwnerContact
PrimaryOwnerName
ResetCapability
ResetCount
ResetLimit
Roles
Status
SupportContactDescription
SystemStartupDelay
SystemStartupOptions
SystemStartupSetting
SystemType
ThermalState
TotalPhysicalMemory
UserName
WakeUpType
Workgroup

Guy Recommends: SolarWinds Engineer’s Toolset (FREE TRIAL)Engineer's Toolset v10

This Engineer’s Toolset provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset on a 14-day free trial now!

SolarWinds Engineer's Toolset Download 14-day FREE Trial

Example 1a – Reading Hostnames and Displaying Data

The purpose of the second part of this page is to reading hostnames from a text file.

Let us assume that the hostnames of the computers that you are interested in are stored in a file called “Guy.txt”.  Each line in the file has one hostname.  This is not an exciting file, but just to be crystal clear are the first three lines of MY file.  Please create YOUR file with names of real computers on your network.

YourMachine
PsychoMachine
GreenServer

PowerShell Script
Now for the PowerShell code, what this does is to read each name, then attempt to run Win32_ComputerName against each machine on YOUR network.


$GuyFile = [System.IO.File]::OpenText("d:\scripts\Guy.txt")
while($Machine = $GuyFile.ReadLine())
{Get-WmiObject -computername $Machine Win32_Computersystem }
$GuyFile.Close()

Note 4: [System.IO.File] creates a file object.  Another interpretation is [System.IO.File]::OpenText(“d:\ scripts\Guy.txt”) gets a handle on the file with the hostnames.

Note 5: To begin with, play safe and include the full path to the file: “d: \scripts\Guy.txt”

Note 6: Trace the variable $GuyFile.  In particular see how we apply the .ReadLine() method.

Note 7: All that is needed to initiate the loop is: ‘while’ as in ‘while($Machine….’. Incidentally I tried foreach and do while, no luck plain ‘while’ was the best.

Note 8: Pay close attention to the brackets.  The while statement needs ( simple) brackets, yet the statement needs {curly} brackets.

Note 9: Examine the -computername $Machine structure.  The switch and the variable are responsible for running the command against different machines in the file.

Note 10: If you create your own cmdlet, remember to finish by closing the file.

Guy Recommends:  Network Performance Monitor (FREE TRIAL)Review of Orion NPM v11.5

SolarWinds Network Performance Monitor (NPM) 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 on a 30-day free trial.

SolarWinds Network Performance Monitor Download 30-day FREE Trial

Example 1b – Reading Hostnames and displaying data (Much tighter code, thanks to /\/\o\/\/)


gc Guy.txt |% {gwmi Win32_Computersystem -computer $_ } | ft Machine, Model, Manufacturer

Note the use of Aliases a) gc: Get-content.  b) gwmi: Get-WmiObject  c) ft: Format-Table

Example 2 – PowerShell Variations of Reading Hostnames

I find that you gain perspective by altering code to achieve the same or similar effects.

a)  I introduce another variable called $TextContainer.
b)  I filter the output to display only Name (Machine), Model and Manufacturer


$TextContainer = "d:\scripts\Guy.txt"
$GuyFile = [System.IO.File]::OpenText($TextContainer)
while($Machine = $GuyFile.ReadLine())
{ Get-WmiObject -computername $Machine Win32_Computersystem `
| Format-Table Name, Model,  Manufacturer}
$GuyFile.Close()

Note 11: The filename, which holds the hostnames is controlled by the variable $TextContainer.

Note 12: While the following command produces a result, | Format-Table Machine, Model,  Manufacturer
Gene Cox points out that a better table is, | Format-Table Name, Model,  Manufacturer

Note  Out-GridView: PowerShell v 2.0 introduces a new cmdlet to control data display.  See more on how to pipe the results into out-GridView.

Summary of Reading Files for PowerShell Scripting

It is often useful to read hostnames from a text file, then persuade PowerShell to loop through a command and finally, display the data in a table.  For this we used the Win32_Computer WMI class.

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


See more PowerShell share examples including WMI

PowerShell WMI   • Create Win32_Share   • WMI Shares   • Free Permissions Analyzer Tool

Get-Acl  • PowerShell Share Error Codes   • Win32_ComputerSystem  • PowerShell 3.0 CIM

Windows PowerShell  • Free WMI Monitor  • Cacls   • Query   • PowerShell Printer Scripts

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