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
♦
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. 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
- Copy and paste the example script below into notepad or a VBScript editor.
- Decide the name of the machine on line 6
- Save Script A with a .vbs extension, for example: ScriptA.vbs
- Double click ScriptA.vbs and check the Message box, click on OK about 6 times.
- Save Script B with a .vbs extension, for example: ScriptB.vbs.
- Note that with ScriptB.vbs you need to once on the OK button.
'Script A - Too many WScript.Echo commands ' OperatingSystem.vbs ' VBScript to document your Operating System ' Author Guy http://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 http://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!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
Download your free copy of WMI Monitor
How to strip the extra WScript.Echo commandsIf 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. 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 Points1) 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.
Their topics and material are ideal for getting you started with VBScript. The
training videos give real-life example and are easy to follow because you can
control the pace.
This structed course is so much
cheaper and more convenient than taking classes. Try their free demo material and then see if you want to buy the full package.
Get a Free Trial of VB Script Training DVD\CD.
See Also
• WMI Secrets • WMI Home • Simple WMI Script •
WMI Basics
|