WMI (Windows Management Instrumentation) – WScript.Echo Technique

Introduction to WMI WScript.Echo Technique

If your script editor or Scriptomatic gives you problems with multiple WScript.Echo commands.  I will show you a technique for removing extra WScript.Echo command and adding VbCr code.

Topics for WMI Secrets

 ♦

Technique – How to Strip Multiple Wscript.Echo commands

The problem.  Too many WSCript.Echo messages.

The situation.  When you use Scriptomatic V2 or a good script editor they create the script for you – good.  However, they insert more WSCript.Echo messages than you really need.  At best this is annoying, at worst it means that you have to press the  OK button 50,000 times.  As a result you begin to wonder if it was worth running the script in the first place.

The solution.  To remove all the WScript.Echo commands except one.  Then to add a carriage return command: & vbCr & _.  Best of all combine the operation with a search and replace.  See what I mean and compare Script A with Script B.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 1 – To display WMI Information about your Machine.

This is just an example script, the contents are not important.  For the record and if you are curious, this script displays information about the machine, its operating system and an service packs.

Prerequisites

This script is suitable for most clients, XP, W2K, even NT 4.0 and Win 9x.

Instructions for Creating your WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide the name of the machine on line 6
  3. Save Script A with a .vbs extension, for example: ScriptA.vbs 
  4. Double click ScriptA.vbs and check the Message box, click on OK about 6 times.
  5. Save Script B with a .vbs extension, for example: ScriptB.vbs. 
  6. Note that with ScriptB.vbs you need to once on the OK button.

Two Scripts to demonstrate the strip WScript.Echo Technique

‘Script A – Too many WScript.Echo commands

‘ OperatingSystem.vbs
‘ VBScript to document your Operating System
‘ Author Guy https://computerperformance.co.uk/
‘ Version 1.3 – June 2010
‘ —————————————-‘
Option Explicit
Dim objWMIService, objItem, colItems
Dim strComputer, strList

On Error Resume Next
strComputer = "."

‘ WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")

‘ WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

‘ For Each… In Loop (Next at the very end)
For Each objItem in colItems
WScript.Echo "Computer: " & objItem.CSName
WScript.Echo "Processor: " & objItem.Description
WScript.Echo "Manufacturer: " & objItem.Manufacturer
WScript.Echo "Operating System: " & objItem.Caption
WScript.Echo "Version: " & objItem.Version
WScript.Echo "Service Pack: " & objItem.CSDVersion
WSCript.Echo ""
Next
WSCript.Quit

‘ End of WMI Win32_OperatingSystem VBScript

 

‘ Script B – Only one WScript.Echo

‘ OperatingSystem.vbs
‘ VBScript to document your Operating System
‘ Author Guy https://computerperformance.co.uk/
‘ Version 1.3 – June 2010
‘ —————————————–‘
Option Explicit
Dim objWMIService, objItem, colItems
Dim strComputer, strList

On Error Resume Next
strComputer = "."

‘ WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")

‘ WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

‘ For Each… In Loop (Next at the very end)
For Each objItem in colItems
WScript.Echo "Computer " & objItem.CSName & vbCr & _
   "Processor: " & objItem.Description & vbCr & _
   "Manufacturer: " & objItem.Manufacturer & vbCr & _
   "Operating System: " & objItem.Caption & vbCr & _
   "Version: " & objItem.Version & vbCr & _
   "Service Pack: " & objItem.CSDVersion & vbCr & _
   ""

Next
WSCript.Quit

‘ End of WMI Win32_OperatingSystem VBScript

 

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

How to strip the extra WScript.Echo commands

If you run ScriptA you only see one piece of information before you have to press O.K. to see the next item.  To see what I mean compare the ScriptA with ScriptB. 

What I like to do is remove all the WSCript.Echo commands, except the first one.  Then I use the power of Word for Windows to Find and Replace.  I search for the ^p (end of line marker) and replace with  & vbCr & _ ^p.VBScript Find and Replace

Next, search for WScript.Echo and replace with nothing.  Remember that you will need just one WScript.Echo at the beginning, and that you don’t need & vbCr & _ ^p on the last line. 

Learning Points

1) I confess my technique is unlikely to work perfectly first time.  However if I have given you the idea I am sure that you can perfect the method.

a) Search for the end of line marker – ^p in Word and replace with:  & vbCr & _ ^p  (note the extra space before the first &).

b) Pay extra attention to the first and last lines.  The first line needs WSCript.Echo, and the last line does NOT need  & vbCr & _ ^p.

2) I liked this technique so much that I even created a macro in Word to run this routine.  Please bear with me and be prepared to adapt this idea to your situation.

 Sub WMI()

‘ WMI Macro to add  & vbCr & _ ^p to each line.

‘ Also to remove WScript.echo.
‘ Macro recorded 6/11/2010 by Guy

Selection.WholeStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = vbTab & "WScript.Echo "
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " & vbCr & _ ^p"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Summary of WMI Technique

With many off-the-shelf WMI Scripts, if you don’t take action and prune the excess WScript.Echo commands then you get wrist  ache constantly pressing the OK button.  It is worth developing my Search and Replace technique to make the output more readable.  The secret is to create a Word for Windows’ macro which utilities Find and Replace.

If you like this page then please share it with your friends

 


See more VBScript WMI examples:

WMI Tutorial   • WMI Who Logged On   • WMI WBEMTest   • Free WMI Monitor   • Free WMI Monitor

WMI Secrets   • VBScript Services   • WMI Techniques   • WMI Scriptomatic

WMI Home   • WMI Moniker   • Import CSVDE – Free Utility   • VBScript Echo   • WMI VBScript