September 3rd, 2008

Hey, Scripting Guy! How Do I Use Windows PowerShell to Run the Same WMI Queries Against Multiple Computers?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to run the same WMI queries against multiple computers on our network. Can I do this with Windows PowerShell?

– KB

SpacerHey, Scripting Guy! Answer

Hi KB,

Not only can you do this with Windows PowerShell, but you will be surprised at how easy it is to do. If you can create a text file in Notepad, you can run your multiple queries against multiple servers. Well, actually, you need two text files and a script, but other than that it is really simple. Here is the code:

$aryquery = Get-Content -path c:\fso\query.txt
$computer = Get-Content -path c:\fso\servers.txt
Foreach ($query in $aryQuery)
{
 Get-WmiObject -Query $query -computername $computer
}

The script begins by reading two separate text files and storing the results into an array of text strings. It then uses a ForEachstatement to walk through each of the queries, and then it performs the query.

To read the text files we use the Get-Content cmdlet and specify the path to the text file. The first text file we read stores the queries we intend to run. Query.txt is seen here:

query.txt image

 

As you can see, the query.txt file is nothing really special. It is simply a collection of WMI queries. As an added bonus, you do not even have to surround the queries in quotation marks, and you don’t need to worry about case-sensitivity issues. The syntax of these queries is exactly the same as you would have used when writing VBScripts. In fact, if it were me (wait, it is me), I would steal some queries from some of my old VBScripts and avoid reinventing the wheel (or in this case the query). You can see the query I stole from my own BLOCKED SCRIPT

VBScript image

 

The next file we need to use is the servers.txt file. This file will contain the names of the servers we wish to query. As seen here,servers.txt file is nothing special either. It is literally a list of server names. No commas are required and neither are quotation marks. By the way, IP addresses would also work in this file:

servers.txt image

 

With our two files out of the way, it is time to begin processing the files. This is a fancy word for “opening them up and taking a gander.” And we don’t mean stealing geese. We use Get-Content to read the content of the text files. Because there are two files, we will be using the Get-Content cmdlet twice, as seen here:

$aryquery = Get-Content -path c:\fso\query.txt
$computer = Get-Content -path c:\fso\servers.txt

We store the content of the query.txt file in the variable $aryquery, and we store the contents of the server.txt file in the $computervariable. You may be wondering why I named one variable $aryquery and the other $computer. Patience, scripthopper. You will understand this soon. When we use Get-Content it always returns an array. In VBScript, when you hear the word “array,” you thinkFOR EACH NEXT. Well, in Windows PowerShell, when you hear the word “array,” you should think ForEach. Because we have an array of WMI queries, we will need to use the ForEach statement to walk through our array of queries. As we go through the array, we will execute one query at a time. We use the variable $query to hold the individual query from our array. This is seen here:

ForEach ($query in $aryQuery)

To execute the WMI queries, we will use the Get-WmiObject cmdlet. We use the -query parameter to supply the query we wish to execute. Now here is where things really get cool. The -computername parameter in the Get-WmiObject cmdlet can take an array! You read this right! It will take an array! That’s three exclamation points! Now four! Okay, I’ll stop. This is why I did not bother to name the contents of the servers.txt file with the ary prefix. I had no intention of iterating through the array. This line of code is seen here:

Get-WmiObject -Query $query -computername $computer

When you run this script, it could produce a lot of output, and you may want to write the contents to a file. You could use either the redirection arrows (>>) or you could use out-file. If you used the >> arrows it would look something like this:

Get-WmiObject -Query $query -computername $computer >> c:\fso\output.txt

(If you have a folder named fso and are writing to a file named output.txt, that’s exactly how the command will look; otherwise, your command will look different.)

So, KB, this is it. This could be the last WMI script you ever need to write. From here on out, all you need to do is to copy the WMI queries from your old VBScripts and paste them into the query.txt file and you are done. Of course, this probably will not be the last WMI script you ever write, because WMI is fun stuff.

Ed Wilson and Craig Liebendorfer, Scripting Guys

Author

0 comments

Discussion are closed.