Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Error Trapping and Capturing Third-Party O...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Error Trapping and Capturing Third-Party Output in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-03-05

    Table of Contents:
  • Error Trapping and Capturing Third-Party Output in WSH
  • Trapping Errors
  • Logging to the Event Log
  • Capturing Third-Party Output

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Error Trapping and Capturing Third-Party Output in WSH - Capturing Third-Party Output


    (Page 4 of 4 )

    Now that we know how to log events in our script in several different ways, let’s take a look at some things worth logging.  One of the most useful purposes for logging in a script is to monitor the activity of a third-party program.

    Perhaps you’re writing a script that runs every hour and tests the TCP/IP connectivity of every computer on your network.  A simple ping test is all that is required.  It seems logical that you would be able to run ping.exe from within your script, but how can you capture its output to determine whether or not it was successful?

    This is where standard streams come into play.  In my last article, “Handling User Input in WSH,” we learned how to use two of the standard streams, standard input (StdIn) and standard output (StdOut).  We’re going to make use of both of those in this segment as well but I am also going to show you the third and final standard stream, standard error (StdErr).

    Before we get too far ahead of ourselves, let’s take a quick look at how to run third-party programs in WSH.  The WshShell object provides us with the Run method and the Exec method.  Both are able to start applications and execute command lines directly.

    The Run method can be compared to running an application from the Run dialog box.  The Exec method is the same as executing a command line at the command prompt.

    When executing command lines there is a slight difference between the two:  the Run method will create a separate instance, or window, for the running process while the Exec method will run within the same instance.  For this reason we will use the Exec method so that output is directed to the same window in which our script is running.

    object.Run strCommand[, intWindowStyle][, bWaitOnReturn]

    object.Exec strCommand

    The Exec method accepts a single string attribute that is the command line to be executed.  It returns a WshShellExec object.  This object has a property for each of the three standard data streams.  Each of these properties returns a read-only text string.

    First, let’s take a look at how to actually execute a command line.  This particular script is going to ping the localhost address.  The script should not fail if you have a network adapter installed.

    strCommand = "ping.exe 127.0.0.1"

     

    Set WshShell = CreateObject("WScript.Shell")

    Set WshShellExec = WshShell.Exec(strCommand)

    Executing this code snippet will not output anything on the screen even though the ping command is executed.  That’s because its output is sent to the WshShellExec object’s StdOut stream.  You can retrieve the information by adding the following lines.

    sOutput = WshShellExec.StdOut.ReadAll

    Wscript.StdOut.Write sOutput

    This code simply reads the WshShellExec’s StdOut stream into a string variable and then writes it to the Wscript object’s StdOut stream which writes it to the command window.

    If we use this example, we can build a basic error-handling routine.

    Const WshFinished = 1

    Const WshFailed = 2

    strCommand = "ping.exe 127.0.0.1"

     

    Set WshShell = CreateObject("WScript.Shell")

    Set WshShellExec = WshShell.Exec(strCommand)

     

    Select Case WshShellExec.Status

       Case WshFinished

           sOutput = WshShellExec.StdOut.ReadAll

       Case WshFailed

           sOutput = WshShellExec.StdErr.ReadAll

    End Select

     

    WScript.StdOut.Write sOutput

    In this code example we check the status of the WshShellExec object.  If it was successful, we grab the standard output stream to get the command’s output.  If it failed, we instead turn to the standard error object to see what went wrong.

    We could easily use the information in sOutput to generate log entries.  You can see now how these two ideas work hand-in-hand.

    I’ve given you some ways to write better, more professional code.  I hope that you’ll take the time to implement them into your own scripts.  Proper error-handling can be very useful if you ever have to provide support for your scripts.  Good luck writing better scripts.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hey, guys. In this article I'm wrapping up my logging concept by showing you how to...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT