Skip navigation

Rem: Running a Command-Line Utility in VBScript

Downloads
25285.zip

Can VBScript run a command-line tool and capture the command's output without redirecting the output to a file?

Yes. Beginning with Windows Script Host (WSH) 5.6, the WSH Shell object provides an Exec method you can use to run a command-line utility and capture the command's output in your script. Listing 1 demonstrates how to use the Exec method.

The script creates a WSH Shell object, then calls the Shell object's Exec method and passes the command name and arguments—ipconfig /all in the sample script—to the Exec method, which returns an instance of the running command as a WshScriptExec object. In Listing 1, I assign this object to the variable named objScriptExec. The WshScriptExec object provides four properties (Status, StdOut, StdIn, and StdErr) and one method (Terminate) to interact with the running command. I use the StdOut property's ReadAll method to capture the ipconfig output in one operation. The script then assigns ipconfig's output to the string variable named strIpConfig and echoes it to the screen. To learn more, see the WSH documentation at http://msdn.microsoft.com/scripting.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish