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.
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.
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.
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.
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\/\/)
Note the use of Aliases a) gc: Get-content. b) gwmi: Get-WmiObject c) ft: format-Table
Guy
Recommends: WMI Monitor and Its Free!
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.
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
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.
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.
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.