PowerShell WMI

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



PowerShell Scripting - WMI Techniques

PowerShell's File Techniques Applied to Win32_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 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 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: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

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 Wind32_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

  ˚

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.

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

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

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.

See more PowerShell share examples including WMI

PowerShell WMI   • Win32_Share   • WMI Shares   • Win32_ComputerSystem   • Query

Get-Acl   • Set-Acl   • PowerShell Create Share   • PowerShell Error Codes

Please write in if you have a better example script. 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.

 *


Google

WebThis Site

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

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

 Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.