Common mistakes and pitfalls

In VBScriptA Visual Basic Scripting language is a high-level programming language developed by Microsoft®. there are two types of variables:

Variable Type Description
Simple Variables are true types like integer, Boolean, string.
Objects Are complex items whose functionality is exposed by the automation objects interface.

It is important to declare the automation object types as Object before assigning them values.

IMPORTANT

It is highly recommended that you assign a value to all declared variables and/or objects.

Example

Function Main

'declare the object to be used

Dim nameStr As String

'assign a value to the variable

nameStr = "This is a test assignment of text"

'display the result in the scanner activity window of the assignment

echo nameStr

'return the result

Main = true

End Function

For a more advanced example, the script below will list which services are installed on the target machine (localhost = 127.0.0.1). Copy paste the following text in the script debugger and run it (F5). In the debug window you will see the list of installed services on the local machine.

Function main

'declare the objects we will need to use

Dim wmi As Object

Dim objswbemobject As Object

'declare other variables we need

Dim strComputer As String

Dim cr As String

strComputer = "127.0.0.1"

'Carriage return

cr = Chr(13) + Chr(10)

'hook with the wmi object

Set wmi = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

'Check that hook was successful

If wmi is Nothing Then echo ("error1")

'Return the services instance of the wmi

Set wmiinst=wmi.instancesof("Win32_Service")

'Check to see that instance is available

If wmiinst is Nothing Then echo ("error2")

'Loop true each instance

For Each objswbemobject In wmiinst

echo("Service Name= ")

'Display services

echo(objswbemobject.DisplayName+cr)

Next

End Function

NOTE

If you try to use an automation object without declaration, the script will fail while compiling . As an example consider the same piece of code but with a missing full declaration of the object variable wmi. The instant you try to run the script you will be presented with an error message as well as a clear indication of the line on which the error occurred:

Function main

Dim wmi 'WARNING : missing “as object”

Dim objswbemobject As Object

End Function