Posts Tagged ‘loop’

How to check the VMware Tools version across your Windows Server 2008 R2 farm…

June 21st, 2012 by Jeremy Pavlov | No Comments | Filed in Microsoft, PowerShell, VMware, VMware ESXi

I was on a conference call today where someone asked if the server administration team could quickly get a “read” on the VMware Tools version across the server farm.  An ESXi upgrade was being deployed and the team wanted a quick way to see what VMs needed to catch up with the deployment.

Fortunately, I had a bunch of PowerShell snippets lying about that would fit the bill nicely.  Of course, there are a few different ways to accomplish this, depending upon which resources that you are permitted to access; but using PowerShell in a loop with a list of your server names is a quick and easy way to get it done.

So first, a little set up…  Imagine that we are inside a PowerShell script, in a foreach loop (or a function called by that loop) that is calling each computer (our Windows 2008 R2 servers) in a list.  At the time you hit this snippet of code, the current server name in the loop is in the variable $ComputerName, and the output file for the report is $outfile

 

    write "" | Out-File $outfile -append
    $vmtoolstatus = Get-WmiObject -class Win32_Product -computername $ComputerName | Where-Object {$_.Name -match "VMWare Tools"} |select Name,version
    if ("X$vmtoolstatus" -eq "X")
    {
      write "VMware Tools **NOT** found." | Out-File $outfile -append
    }
    else
    {
      write "WMware tools presence/version info:" | Out-File $outfile -append
      $vmtoolstatus | Out-File $outfile -append
    } 

 

All we’re really doing here is to query the product list for the tools, and if present report the version. And the result look something like this for each item in the loop:

WMware tools presence/version info:
Name                                                        version                                                  
----                                                        -------                                                    
VMware Tools                                                8.3.2.1593

Simple, but effective and functional. 

I hope it helps!

 

 

Did you like this? Share it:

Tags: , , , , , , ,

More DOS batch file fun…

April 19th, 2012 by Jeremy Pavlov | No Comments | Filed in Microsoft, Scripting

Subtitle: “Getting more precise and limited output from FindStr results against multiple files…” …which is a lousy subtitle, I know.

Sometimes, you just want to parse multiple files in a folder for particular lines of content, and do something with those parsed results (perhaps dump to file, or use as input for a loop). However, one of the challenges can be that while using FindStr against multiple files, the output shows the source filename on the line with the result as a courtesy (thanks a lot). And lacking Linux-like flexibility of control over the command options, we’re almost out of luck before we ever start — but there are ways around that… First, however, I’ll demonstrate the challenge.

DOS is Not Linux

Starting out with a test folder, containing two text files each with a few lines we want, and few we don’t.

C:\Test>dir /B
file.wri
other.wri

Assume that we are looking for any line in these files that contains the tar command; so using findstr, we issue our search:

C:\Test>findstr /C:"tar " *.wri

…and as described, the result shows the filename on each line:

file.wri:tar this
file.wri:tar that
other.wri:tar some other stuff
other.wri:#commented out tar thingy

This would not be good if we were trying to take the results as issued commands or as input to other commands. So, we’ll try to meet findstr on its own terms, and using good ol’ type and try to mask the fact that we are sending individual files to findstr.

So piping the one into the other, we issue the command:

C:\Test>type *.wri |findstr /C:"tar "

…and the the result is just goofy:

file.wri

other.wri

tar this
tar that
not a line you want tar some other stuff
#commented out tar thingy

But DOS Loops Rule

But all hope is not lost. If we loop through all the files individually, and stuff them each into findstr one-by-one with each iteration of the loop, findstr will be happy and give us pretty, clean, limited output. Yay! Just put this in a script to see what I mean:

@ECHO OFF
@For /F "tokens=*" %%Q in ('dir /B c:\test\*.wri') Do @(
findstr /C:"tar " %%Q
)

…and of course, the result is exactly what I wanted:

tar this
tar that
tar some other stuff
#commented out tar thingy

Enjoy!

Did you like this? Share it:

Tags: , , , , , ,