ActiveXperts Network Monitor
Monitor servers, workstations, devices and applications in your network

Quicklinks


NOTE: ActiveXperts Network Monitor ships with a large collection of VBScript scripts to monitor any aspect of your network. Most VBScript scripts also have a PowerShell implementation. Download Now »


Scripting Techniques

Adding Elements to a Dictionary
Creating an Instance of Internet Explorer
Creating Script Documentation Using Script Comments
Determining the Number of Items in a Dictionary
Displaying Real Time Events in a Command Window
Displaying Tabular Output in a Command Window
Masking Command Line Passwords
Masking Passwords Using Internet Explorer
Removing All Elements from a Dictionary
Removing Debugging Comments
Removing One Element from a Dictionary
Retrieving Command Line Arguments from an Active Directory Container
Retrieving Command Line Arguments from a Text File
Retrieving a Web Page
Saving Data in XML Format
Sorting WMI Data
Suppressing Multiple Event Notifications
Tracking Script Progress in a Command Window
Tracking Script Progress Using Internet Explorer
Using a Text File as a Command Line Argument
Verifying the Existence of a Dictionary Key

Adding Elements to a Dictionary


Demonstration script that adds three key-item pairs to a Script Runtime Dictionary. Script must be run on the local computer.
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Printer 1", "Printing"   
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"

Creating an Instance of Internet Explorer


Demonstration script that creates an instance of Internet Explorer, opened to a blank page.
Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=300
objExplorer.Height = 150 
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Visible = 1

Creating Script Documentation Using Script Comments


Demonstrates the use of the FileSystemObject as a way to copy comments from a script to a separate text file. Requires comments to have been marked using '*.
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objScriptFile = objFSO.OpenTextFile("c:\scripts\Service_Monitor.vbs", _
    ForReading)
Set objCommentFile = objFSO.OpenTextFile("c:\scripts\Comments.txt", _ 
    ForWriting, TRUE)
Do While objScriptFile.AtEndOfStream <> TRUE
    strCurrentLine = objScriptFile.ReadLine
    intIsComment = Instr(1,strCurrentLine,"'*")
    If intIsComment > 0 Then
        objCommentFile.Write strCurrentLine & VbCrLf
    End If
Loop
objScriptFile.Close
objCommentFile.Close

Determining the Number of Items in a Dictionary


Demonstration script that counts the number of key-item pairs in a Script Runtime Dictionary. Script must be run on the local computer.
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Printer 1", "Printing"   
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
Wscript.Echo objDictionary.Count

Displaying Real Time Events in a Command Window


Creates a temporary event consumer that monitors the event log for error events. When an error event occurs, the script displays the event information in the command window.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Security)}!\\" & _
        strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("Select * from __InstanceCreationEvent within 5 where TargetInstance isa " _
        & "'Win32_NTLogEvent' and TargetInstance.EventType = '1'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
        Wscript.Echo "Record No.: " & _
            objLatestEvent.TargetInstance.RecordNumber
        Wscript.Echo "Event ID: " & objLatestEvent.TargetInstance.EventCode
        Wscript.Echo "Time: " & objLatestEvent.TargetInstance.TimeWritten
        Wscript.Echo "Source: " & objLatestEvent.TargetInstance.SourceName
        Wscript.Echo "Category: " & _
            objLatestEvent.TargetInstance.CategoryString
        Wscript.Echo "Event Type: " & objLatestEvent.TargetInstance.Type
        Wscript.Echo "Computer: " & _
            objLatestEvent.TargetInstance.ComputerName
        Wscript.Echo "User: " & objLatestEvent.TargetInstance.User
        Wscript.echo "Text: " & objLatestEvent.TargetInstance.Message
Loop

Displaying Tabular Output in a Command Window


Retrieves service data from a computer, and then outputs that data in tabular format in a command window.
Set colServices = GetObject("winmgmts:"). _
    ExecQuery("Select * from Win32_Service")
For Each objService in colServices
    intPadding = 50 - Len(objService.DisplayName)
    intPadding2 = 17 - Len(objService.StartMode)
    strDisplayName = objService.DisplayName & Space(intPadding)
    strStartMode = objService.StartMode & Space(intPadding2)
    Wscript.Echo strDisplayName & strStartMode & objService.State 
Next

Masking Command Line Passwords


Demonstration script that uses ScriptPW.dll to mask passwords entered at the command line.
Set objPassword = CreateObject("ScriptPW.Password") 
WScript.StdOut.Write "Please enter your password:" 
strPassword = objPassword.GetPassword() 
Wscript.Echo
Wscript.Echo "Your password is: " & strPassword

Masking Passwords Using Internet Explorer


Demonstration script that creates an instance of Internet Explorer, and retrieves a password typed into a password-style text box. Requires a Web page named password.htm with the appropriate text box.
Set objExplorer = WScript.CreateObject _
    ("InternetExplorer.Application", "IE_")
objExplorer.Navigate "file:///c:\scripts\password.htm"   
objExplorer.Visible = 1             
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=400
objExplorer.Height = 250 
objExplorer.Left = 0
objExplorer.Top = 0
Do While (objExplorer.Document.Body.All.OKClicked.Value = "")
    Wscript.Sleep 250                 
Loop 
strPassword = objExplorer.Document.Body.All.PasswordBox.Value
objExplorer.Quit
Wscript.Sleep 250
Wscript.Echo strPassword

Removing All Elements from a Dictionary


Demonstration script that deletes all the key-item pairs from a Script Runtime Dictionary. Script must be run on the local computer.
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Printer 1", "Printing"   
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
colKeys = objDictionary.Keys
Wscript.Echo "First run: "
For Each strKey in colKeys
    Wscript.Echo strKey
Next
objDictionary.RemoveAll
colKeys = objDictionary.Keys
Wscript.Echo VbCrLf & "Second run: "
For Each strKey in colKeys
    Wscript.Echo strKey
Next

Removing Debugging Comments


Demonstrates the use of the FileSystemObject as a way to remove debugging comments from a script. Requires comments to have been marked as '* BUG.
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Scripts\CreateUser.vbs", ForReading)
 
Do While objTextFile.AtEndOfStream <> true
    strNextLine = objTextFile.Readline
    intCheckForBugComment = Instr(strNextLine, "'* BUG")
    If intCheckForBugComment = 0 Then
        strSavedLines = strSavedLines & strNextLine & VbCrLf
    End If
Loop
 
Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\CreateUser.vbs ", ForWriting)
objTextFile.Write strSavedLines 
objTextFile.Close

Removing One Element from a Dictionary


Demonstration script that deletes a specific key-item pair from a Script Runtime Dictionary. Script must be run on the local computer.
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Printer 1", "Printing"   
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
colKeys = objDictionary.Keys
Wscript.Echo "First run: "
 
For Each strKey in colKeys
    Wscript.Echo strKey
Next
 
objDictionary.Remove("Printer 2")
colKeys = objDictionary.Keys
Wscript.Echo VbCrLf & "Second run: "
 
For Each strKey in colKeys
    Wscript.Echo strKey
Next

Retrieving Command Line Arguments from an Active Directory Container


Demonstration script that retrieves the names of all the computers in an Active Directory container, and then returns service information from each of those computers.
Set objDictionary = CreateObject("Scripting.Dictionary")
i = 0
Set objOU = GetObject("LDAP://CN=Computers, DC=fabrikam, DC=com")
objOU.Filter = Array("Computer")
For Each objComputer in objOU 
    objDictionary.Add i, objComputer.CN
    i = i + 1
Next
For Each objItem in objDictionary
    Set colServices = GetObject("winmgmts://" & _
        objDictionary.Item(objItem) _
            & "").ExecQuery("Select * from Win32_Service")
    Wscript.Echo colServices.Count
Next

Retrieving Command Line Arguments from a Text File


Demonstration script that opens a hypothetical text file consisting of server names, then retrieves service information from each on the servers in the file.
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\servers.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream 
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop
For Each objItem in objDictionary
    Set colServices = GetObject("winmgmts://" & _
        objDictionary.Item(objItem) _
            & "").ExecQuery("Select * from Win32_Service")
    Wscript.Echo colServices.Count
Next

Retrieving a Web Page


Retrieves the HTML source for the Web page http://www.microsoft.com. This script contributed by Maxim Stepin of Microsoft.
url="http://www.microsoft.com"
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
Call objHTTP.Open("GET", url, FALSE)
objHTTP.Send
WScript.Echo(objHTTP.ResponseText)

Saving Data in XML Format


Demonstration script that retrieves service information for a computer, and then saves that data as an XML file.
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\service_status.xml", ForAppending, True)
objTextFile.WriteLine ""
objTextFile.Write ""
objTextFile.WriteLine ""
strComputer = "."
Set colServices = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"). _
        ExecQuery("Select * from Win32_Service")
For Each objService in colServices    
    objTextFile.WriteLine ""
    objTextFile.WriteLine ""
    objTextFile.WriteLine objService.DisplayName
    objTextFile.WriteLine ""
    objTextFile.WriteLine ""
    objTextFile.WriteLine objService.State
    objTextFile.WriteLine ""
    objTextFile.WriteLine ""
Next
objTextFile.WriteLine ""
objTextFile.Close

Sorting WMI Data


Demonstration script showing how WMI data can be sorted using a disconnected recordset (by itself, WMI does not allow you to specify a sort order for returned data). In this script, service information is retrieved using WMI and is stored in a disconnected recordset, a recordset that is not tied to a physical data source. The Sort method is then used to sort the service data by service state rather than by service name.
Const adVarChar = 200
Const MaxCharacters = 255
 
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "ServiceName", adVarChar, MaxCharacters
DataList.Fields.Append "ServiceState", adVarChar, MaxCharacters
DataList.Open
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set ServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
 
For Each Service in ServiceList
    DataList.AddNew
    DataList("ServiceName") = Service.Name
    DataList("ServiceState") = Service.State
    DataList.Update
Next
 
DataList.Sort = "ServiceState"
DataList.MoveFirst
 
Do Until DataList.EOF
    Wscript.Echo DataList.Fields.Item("ServiceName") _
        & vbTab & DataList.Fields.Item("ServiceState")
    DataList.MoveNext
Loop

Suppressing Multiple Event Notifications


Issues an alert if available space on a disk drive falls below 100 megabytes. Will wait one hour before issuing the next alert.
dtmStartTime = Now
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objDiskDrives = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")
For Each objDrive in objDiskDrives
    If objDrive.FreeSpace < 10000000 Then
        Wscript.Echo "Drive is low on disk space."
    End If
Next
Do
Set objDiskDrives = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk")
For Each objDrive in objDiskDrives
    If objDrive.FreeSpace < 10000000 Then
        intElapsedHours = DateDiff("h", dtmStartTime, Now)
            If intElapsedHours >= 1 Then
                Wscript.Echo "Drive is low on disk space." 
            dtmStartTime = Now
        End If  
    End If
Next
Wscript.Sleep 1000
Loop

Tracking Script Progress in a Command Window


Demonstrates the use of StdOut as a method for indicating the progress being made by a script.
Wscript.Echo "Processing information. This might take several minutes."
strComputer = "."
Set colServices = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"). _
    ExecQuery("Select * from Win32_Service")
For Each objService in colServices
    Wscript.StdOut.Write(".")
Next
Wscript.StdOut.WriteLine
Wscript.Echo "Service information processed."

Tracking Script Progress Using Internet Explorer


Demonstrates how to use Internet Explorer as a method for indicating the progress being made by a script.
Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"   
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width=400
objExplorer.Height = 200 
objExplorer.Left = 0
objExplorer.Top = 0
Do While (objExplorer.Busy)
    Wscript.Sleep 200
Loop    
objExplorer.Visible = 1             
objExplorer.Document.Body.InnerHTML = "Retrieving service information. " _
    & "This might take several minutes to complete."
strComputer = "."
Set colServices = GetObject("winmgmts: \\" & strComputer & "\root\cimv2"). _
    ExecQuery("Select * from Win32_Service")
For Each objService in colServices
    Wscript.Sleep 200
Next
objExplorer.Document.Body.InnerHTML = "Service information retrieved."
Wscript.Sleep 3000
Wscript.Quit

Using a Text File as a Command Line Argument


Demonstration script that allows you to drag a text file (consisting of server names) onto the script icon in Windows Explorer. The script then opens the text file, then retrieves service information from each on the servers in the file.
Set objArgs = WScript.Arguments
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)
i = 0
 
Do While objTextFile.AtEndOfStream <> True
  strNextLine = objTextFile.Readline
  objDictionary.Add i, strNextLine
  i = i + 1
Loop
 
For Each objItem in objDictionary
  Set colServices = GetObject("winmgmts://" & objDictionary.Item(objItem) _
      & "").ExecQuery("Select * from Win32_Service")
  Wscript.Echo colServices.Count
Next

Verifying the Existence of a Dictionary Key


Demonstration script that verifies the existence of a particular key within a Script Runtime Dictionary. Script must be run on the local computer.
Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.Add "Printer 1", "Printing"   
objDictionary.Add "Printer 2", "Offline"
objDictionary.Add "Printer 3", "Printing"
If objDictionary.Exists("Printer 4") Then
    Wscript.Echo "Printer 4 is in the Dictionary."
Else
    Wscript.Echo "Printer 4 is not in the Dictionary."
End If