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

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 also more PowerShell WMI topics

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

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

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

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

Please report a broken link, or an error.