Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


June 2005

Script Your Own Desktop Diagnostic Tool

Build a tool to troubleshoot PC problems and learn about command shell scripting, too
RSS
Subscribe to Windows IT Pro | See More Resource Kit Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Assembling Script Elements
After you've identified the utilities you plan to use, you need to assemble the utility commands into a code sequence that gives you the report output you want. I typically proceed by choosing the simplest capture scenario (i.e., number 1 from the list above) and concentrate on addressing that one scenario. After the local version of the script is working, I leverage that code to handle the other capture scenarios. DesktopDiag.bat's basic syntax format follows below. It contains a heading line followed by a blank line, then includes the output of a command or utility followed by another blank line. The double-redirect greater than (i.e., >>) characters send the output to a file in an append mode. The file is created if it doesn't already exist; if the file exists, the information is appended to the end of the file. A single redirect character (i.e., >) creates a file, overwriting any old data if a file of the same name exists.

The following basic syntax is used for the 14 utilities and built-in commands I've identified:

ECHO ********** Title Information Line **********>>"%file%"
ECHO.>>"%file%"
C:\UtilLoc\ToolName.exe >>"%file%"
ECHO.>>"%file%"

Listing 1 gives an example of how the syntax is implemented in a script with the Microsoft Windows 2000 Resource Kit's Gpresult.exe tool.

To address the second capture scenario, DesktopDiag.bat retrieves data from a remote machine. Fortunately, gone are the days when most commands and tools would run only locally. The built-in remote functionality of tools such as the Microsoft Windows Server 2003 Resource Kit's Srvinfo.exe and Sysinternals' PsInfo and the availability of remote execution tools such as PsExec and BeyondExec enables remote queries that weren't possible only a few years ago. The code for data capture from a remote machine is very similar to the code for capture from a local machine but adds N/A comments for some commands that don't work well remotely. Note the breakout point for the remote code in Listing 2. If the script operator doesn't specify an argument at runtime, the script runs locally. If a single argument is specified, the script flow jumps to the remote section of the code.

The third capture scenario occurs when a username is specified and used in place of the logged-on username in a remote machine query. The username input is a <domain\userID> argument specified at script runtime that immediately follows the target computer PC name argument.

While we're on the subject of specified arguments, one of the toughest but most necessary aspects of script coding is determining whether user input is flawed, particularly if you need to hand off the script to someone who might not be familiar with scripting. If I hadn't included error handling code in DesktopDiag.bat and you ran the script with an incorrect computer name or username, or if you specified a PC that wasn't online, the script would run through the code returning errors and produce a mystifyingly blank report.

To return to the third capture scenario, one of the first things to do is test for the existence and the online condition of the PC. Callout A in Listing 3 contains the "If Exist" code to test for the PC's existence. If the C$ share on the remote computer doesn't exist, the script terminates with an error message that tells the operator to check the computer's name or online condition. The code at Callout C determines whether the user account exists by checking it with joeware's GetUserInfo utility. If GetUserInfo returns the string "memberships" as part of its output, it has located a valid user account and is returning group memberships for the user. If GetUserInfo doesn't return "memberships," the script terminates and sends an error message to the operator. This operation prevents wasting time running the utility with bogus input. Some tools can seem to take forever to error out if input is incorrect.

Regarding the user account specified as a second argument in Listing 3, note the breakout point where the script flow detects the second argument in the code at callout B. First, the DUres variable is left blank, then made equal to the %2, or second script argument. The "If defined" code tests whether the DUres variable has content, then the For command breaks that string into the domain and username information that's tested against the GetUserInfo utility.

Capturing data on a remote machine while multiple users are logged on locally is the fourth capture scenario. You might be thinking, "How can a PC or server node have multiple local logons?" Terminal Services makes it possible for multiple users to log on locally to a node at the same time. Because DesktopDiag.bat has no idea which user you intend in the user portion of the code, you must work around this potential problem. You can use the Win2K resource kit's Findgrp tool with a For loop to capture group memberships for multiple users. However, if we run the Gpresult tool to get the logged in user's RSoP, the tool won't return accurate results because each time it runs it will query only against the console user's account. Consequently, the script must detect whether there are multiple logged-on users and disable Gpresult for the user policies if multiple local users are detected. Listing 4 contains the For command code that uses the usercntr variable to count the number of users who are logged on locally. Listing 5 contains the If command code that disables the Gpresult utility's run portion of the code if more than one user (i.e., if %usercntr% GTR 1) has been detected.

Testing the Script and Deploying to Support Technicians and Help Desk Teams
Extensive testing in your environment is the logical prior step to take before training users on and deploying the DesktopDiag.bat script. After you configure the script and place it in a shared location, you can make a trial run on a test box. Be sure to test the script under the four capture scenarios I've discussed. After you've tested the script, you might deploy it selectively so that you can easily observe the results and make changes as required. After the script is working to your satisfaction and you have any necessary management approval, you can fully deploy it to your Help desk team and other support technicians.

I've tested the DesktopDiag.bat script code on Windows Server 2003, Win2K Server Service Pack (SP) 3, Win2K SP3, and Windows XP Professional SP1. You can see the kind of output the script produces in the sample report file WORK1-diag.txt, which is available in the 46268.zip file. Here are step-by-step instructions for deploying the script.

  1. Download the DesktopDiag.bat script.
  2. Download or otherwise obtain the 14 utilities that the script depends on. Table 1 lists the utilities and where to obtain them.
  3. Place the utilities in a shared folder with no spaces in the pathname. Configure the path as follows:
    Set UtilLoc=\\Sales\DiagUtilities
  4. To run the script on the local machine, use the syntax

     DesktopDiag.bat
    with no arguments.
  5. To run the script on a remote node, use the syntax
    DesktopDiag.bat <pcname>
    for example: C:\Scripts\DesktopDiag.bat pc5559000
  6. To run the script on a remote node and force a user account, use the syntax
    DesktopDiag.bat <pcname> <domain\userID>
    for example: C:\Scripts\DesktopDiag.bat pc5559000 sales\rlewis
  7. The script will run and the output will pop up in Internet Explorer (IE) if IE is installed in a default location. If IE isn't installed in a default location or you use an alternate browser, you'll need to modify the code to either point to the alternate browser or open the output in Notepad or Wordpad.
  8. The output text file will remain in the user's Temp folder if you leave the code as written. If you want to delete, move, or copy the text file, uncomment or modify the copy and delete code. (I'll show you how to do so shortly.)

Most configuration items will display correctly when you run DesktopDiag.bat on a local PC, even if the logged on user lacks local Administrator permissions. To run remote queries, you must have Administrator permissions on the target PCs.

You have several options for displaying the diagnostic results output from DesktopDiag.bat. At the conclusion of the script run, the code opens IE to display the output. If you prefer to use a different browser or another application to open the report file, you can modify the script so that it opens the report file with an alternate browser, Notepad, or WordPad. After the script creates and displays the output file, the file remains in the user's Temp folder—you can move or copy the file to an administrative location for retention or further analysis or delete it. If you want to delete the text output file after closing IE, uncomment the following line of code:

rem del "%file%"

If you want to move the text output file to another location after closing IE, uncomment the following line of code:

rem Move "%file%" "\\servernamesharename\%computername%-diag.txt"

If you want to copy the text output file to another location after closing IE, uncomment the following line of code:

rem Copy "%file%" "\\servernamesharename\%computername%-diag.txt"

You can deploy DesktopDiag.bat on a removable drive such as a mini USB drive or a floppy drive. Alternatively, you can locate the script on a central share location, such as the shared folder in which you stored the utilities.

Modifying the Script to Capture Additional Data
One of the advantages of scripting your own solution is that you can always add or remove scope quickly, particularly if you locate your script in a shared folder so that you can change the code in one location and all potential users will have access to your update. When the latest virus attack or spyware slips in under the radar, you can use DesktopDiag.bat to look for signature files or registry settings. When you have additional information needs, just add additional code lines following the basic syntax format I've shown you.

The DesktopDiag.bat script should prove to be an extremely flexible and useful utility in your troubleshooting toolkit. In just a minute or two it can query problem PCs and help you eliminate or at least shorten support technician visits.

Project Snapshot: How to
PROBLEM: Troubleshooting user and PC problems can be frustrating for support technicians and Help desk team members.
WHAT YOU NEED: Output wish list, DesktopDiag.bat script, utilities to return desired output
DIFFICULTY: 1.5 out of 5
PROJECT STEPS:
  1. Compile an output wish list.
  2. Identify the tools that will obtain the data you want to capture.
  3. Identify capture scenarios.
  4. Assemble your script elements.
  5. Test the script and deploy it to support technicians and your Help desk team.
  6. Modify the script to capture additional data.



End of Article

   Previous  1  [2]  Next  


Reader Comments
D

middledd April 28, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Learning Path To learn more about command shell scripting, check out:
"“Rem: Learning about Windows Shell Scripting"

"“Shell Scripting 101 Lessons 1 through 10"


To learn more about specific shell scripting tools, take a look at:
"“7 Cool Command-Shell Scripting Tools"

"“Command-Line Scripting Tools in Windows 2003"

"“Command-Shell Tool Resources"

"“Creating a Shell Scripting Tools Menu System"


Access a great resource for command shell–troubleshooting script code:
"Jerold Schulman’s JSI FAQ Web site"


Top Viewed ArticlesView all articles
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

Google's Browser Created Out of Fear of Microsoft

A deep fear of Microsoft drove Google to create its own Web browser, the company's cofounders implicitly admitted Tuesday, though each was careful never to mention the software giant by name. Instead, during a press conference, Google's leaders discussed ...

Let's Get Out of the (Network) Neighborhood

Network Neighborhood might've made sense way back when, but it's long past obsolete today. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Are You Satisfied?

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.

Job Openings in IT


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

IT Connections
Dive into the new Microsoft platforms and products you implement and support with the experts from Microsoft, TechNet Magazine, Windows ITPro and industry gurus. There are 70+ sessions and interactive panels with networking opportunities.

Attention User Group Leaders...
Announcing the eNews Generator—a FREE HTML e-newsletter builder for user group leaders. Build your HTML and text e-newsletters in minutes and add Windows IT Pro & SQL Server Mag articles alongside your own message!.

Master SharePoint with 3 eLearning Seminars
Learn how to build a better SharePoint infrastructure and enable powerful collaboration with MVPs Dan Holme and Michael Noel. Register today!

Get SQL Server 2008 at WinConnections
Don’t miss Microsoft Exchange and Windows Connections conferences, the premier events for Microsoft IT Professionals in Las Vegas, November 10-13. Every attendee will receive a copy of SQL Server 2008 Standard Edition with one CAL.



Order Your SQL Fundamentals CD Today!
Learn how to use SQL Server, understand Office integration techniques and dive into the essentials of SQL Express and Visual Basic with this free SQL Fundamentals CD.

Virtualization Congress Oct. 14-16 in London
Don't miss Virtualization Congress, the premiere EMEA conference dedicated to hardware, OS and application virtualization. Oct. 14-16.
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technical Resources Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing