Windows PowerShell


PowerShell Scripting - WMI File Techniques

Introduction to PowerShell's WMI File Techniques

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 / WMI script.

WMI and PowerShell Topics

 ♣

Get-WmiObject

The heart of the command is a simple statement
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

WmiObject get-Member

Never miss an opportunity to learn about an object by adding get-Member:
get-WmiObject win32_computersystem | get-Member.

One reason for discovering the properties of an object is to concentrate on specific items rather than being swamped by a list of all possible properties.  Incidentally, you can even refer to get-Member by its alias (gm) for example:

get-WmiObject win32_Bios | gm
get-WmiObject win32_LogicalDisk | gm
get-WmiObject win32_MemoryDevice | gm

Special note, the pipeline symbol displays as ¦ in MSH, but as | in notepad.

Reading from the text file

Let us assume that the hostnames of the computers that you are interested in are stored in a text file called "Guy.txt".  Each line in the file has one hostname.  This is not an exciting file, but just to be crystal clear here is the first three lines:

Hostname1
PsychoMachine
GreenMachine

Now for the PowerShell code, this is more exciting.

Example 1a - Reading hostnames and displaying data

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

Note 1: [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 2: To begin with, play safe and include the full path to the file: "d: \scripts\Guy.txt"

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

Note 4: 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 4: Pay close attention to the brackets.  The while statement needs ( simple) brackets, yet the statement needs {curly} brackets.

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

Note 6: 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

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 1: The filename, which holds the hostnames is controlled by the variable $TextContainer.

Note 2: The pipeline symbol displays as ¦ in MSH, but as | in notepad.

Note 3: 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.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

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 WMI class and finally, display the data in a table.

See more Microsoft PowerShell examples of WMI

PowerShell Home   • WMI   • Service   • WMI File   • WMI Query   • DNS   • Disk

Classes   • Network Adapter   • PowerShell WMI examples

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.