PowerShell Scripting - WMI File TechniquesIntroduction to PowerShell's WMI File TechniquesOur 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-WmiObjectThe heart of the command is a simple statement Other variations of get-WmiObject that you could try are:
get-WmiObject
win32_Bios WmiObject get-MemberNever miss an opportunity to learn about an object by
adding 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 Special note, the pipeline symbol displays as ¦ in MSH, but as | in notepad. Reading from the text fileLet 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 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") 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 hostnamesI find that you gain perspective by altering code to achieve the same or similar effects. a) I introduce another variable called $TextContainer.
$TextContainer = "d:\scripts\Guy.txt" 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 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 v10
|
||||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |