Gather Online/Offline Statistics for Your PC Community

Knowledge is power

Dick Lewis

August 15, 2004

5 Min Read
ITPro Today logo


Here's a familiar—and frustrating—scenario: Your IT team needs to deploymultiple Windows hotfixes on severalhundred PCs overnight. But you can'tpush the patches out to all the PCsbecause inevitably, some users shutdown their systems at the end of theday. If you can't force users to leavetheir PCs on, you can at least obtainstatistics about your user community,such as how many PCs are on at anygiven time, which can improve yourchances of successful deployments.

I wrote a script that provides suchinformation. GetNodePingStatus producesa report that tells how many PCs(i.e., nodes) are on at any time. You canalso use the script to ping a list ofnodes and generate a report that tellsyou which of those nodes are offline.GetNodePingStatus removes all pingablenodes from the list each time itruns. By running the script for severaldays, you'll develop an accurate pictureof how many nodes are online(and when) and which nodes arenever on or might not even exist. Thisinformation can help you determinehow much time you'll need to push ahotfix out to the greatest number ofPCs. You can also use the statistics thatGetNodePingStatus provides to cleanup your Active Directory (AD) or PCinventory-control system by deletingdefunct nodes.

Script Breakdown
GetNodePingStatus begins by obtaininga date and timestamp and settingthe on and off counters to zero. Thescript uses the counters to determinethe total number of nodes it's successfullypinged. After GetNodePingStatusperforms these initial tasks, it executeseither the nonincrementing or incrementingsection of code, dependingon the mode in which you run thescript. The default is nonincrementalmode, which returns statistics aboutthe nodes' on or off status and echoesthem to the log file once each time thescript is run. In incremental mode, thescript rotates the output file (i.e.,reuses it as an input file) so that it cangenerate a running list of unsuccessfullypinged nodes on any run of thescript.

In both the nonincremental andincremental modes, GetNodePing-Status performs three simple steps:

  1. Retrieves a node name from theinput file, which contains one nodename per line. The code at callouts Aand D in Listing 1 performs thisaction.

  2. Performs a Ping test on that node,as the code at callouts B and E shows.

  3. Logs the results to a file. In incrementalmode, the script uses theresults as an input file, as the code atcallout C shows. In nonincrementalmode, the script distributes thoseresults, as the code at callout F shows.

I use some special coding techniquesin GetNodePingStatus. Iinclude code to handle spaces in pathnamesfor input and output files andto rotate the input file. In addition tothese techniques, I also reuse somecode in the incrementing and nonincrementingsections. (For more informationabout reusing code, see thesidebar "Expanding Scope for YourScripts,")

Handling spaces.Coding to accommodatespaces in the input and outputfile pathnames is problematic.You can code the output file pathnameby encapsulating the output variable in double quotes. The following codeechoes the %results% variable's valueto the output file specified in the Setcommand:

Set output=D:myscripts  output file.txtEcho %results%>>"%output%"

Coding to handle spaces in input filepaths is trickier because the For commanddoesn't support spaces in theinput file pathname. The followingcode works as long as the pathnamehas no spaces but fails if the pathnameincludes a space:

Set InputList1=  D:myscriptsodelist.txtFor /f "tokens=1,*" %%i in  (%InputList1%) Do  (Set Target=%%i) &  (Call :Pingit)

Fortunately, in Windows 2000 andlater, the For command supports theusebackq switch, which lets you specifythe use of double quotes to encapsulatea file path to be parsed so thatthe double-quoted contents aren'ttreated as a string or command to beexecuted. The following sample commandshows how to use usebackq andsuccessfully handles paths with andwithout spaces:

Set InputList1=  D:myscriptsodelist.txtFor /f "tokens=1,* usebackq"  %%i in ("%InputList1%") Do  (Set Target=%%i) &  (Call :Pingit)

Rotating the input file. Typically,you use a static input file in scripts.This input file might contain a list ofservers or PCs on which you want toperform an operation. In the incrementingsection of GetNodePingStatus,I use the output file that containsa list of failed nodes, which was createdin the previous script run, as theinput file on the next run.

You can use any of several methodsto rotate the input file. Regardless of themethod you use, though, you need a working copy of the original input fileso that you can rerun the script fromthat starting point. If the script modifiesthe original input file, you might not beable to recreate it or it might not matchthe original file. When you run GetNodePingStatusand it doesn't find theworking copy, this simply means thatthe script is on a first run and will createthe working copy. The Move commandat callout C overwrites theprevious working copy of the input fileand prepares the script for the next run.

Using GetNodePingStatus
To use GetNodePingStatus, first downloadthe GetNodePingStatus.bat scriptfrom the Windows Scripting SolutionsWeb site at http://www.winnetmag.com/windowsscripting, enter Instant-Doc ID 43310 in the InstantDoc ID box, then click the 43310.zip hotlink.(Column widths in the printed publicationforce us to wrap code lines,which might cause the printed code torun incorrectly.) Then, create a folderlocation for the script. The script willautomatically locate and create thereport files in that folder location.

By default, the output files will becomma-separated value (.csv) filesthat you can open and edit in MicrosoftExcel. If you want to use a text ortab-separated value (.tsv) file format,you need to modify the script andchange the output file extension to .txtor .tsv, respectively.

As I mentioned earlier, you can runGetNodePingStatus in incrementalmode or nonincremental mode. Tolaunch the script in incremental node,run the command

getnodepingstatus.bat -inc

The -inc switch specifies incrementalmode. To run the script in the defaultnonincremental mode, run the samecommand without the -inc switch.

Useful Information
GetNodePingStatus lets you gathervaluable statistics about your PCs'online and offline status. In doing so,you might learn some surprisinginformation about your PC community.For instance, when I ran Get-NodePingStatus in my organization, Idiscovered that many more of ourusers' PCs were on at night than Iexpected. I also discovered that manyPCs in our active inventory list weren'tactually online. This discovery motivatedus to reevaluate the validity of our inventory-control system information.When you run GetNode-PingStatus, you'll probably discoversome interesting trends in your ownenvironment.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like