Scripting in Visual Basic

If you choose to start writing your script as a Visual Basic program, use the following procedure.

To create a Visual Basic program for SMS operations

  1. Start Visual Basic, and then create a new project.

  2. On the Project menu, click References, and then click Microsoft WMI Scripting Vx.x Library.

  3. In the "(General)", "(Declarations)" section of your program, add

    Private WbemServices as SWbemServices
  4. In a new subroutine (or function), add

    Dim loc as New SWbemLocator.
  5. Add

    Set WbemServices = loc.ConnectServer( , "root\SMS\site_<site code>")
    (add the server name as the first parameter and the user name and password as the third and fourth parameters (if necessary) if the script will run from a computer other than the site server).
  6. Wherever you need variables for the WMI objects, include

    Dim <variable name> as SWbemObject

The Visual Basic program is now ready to work with WMI, and thus with SMS objects. Examples from this appendix can be used in the program (except that the lines to create a locator and connect to the WMI server do not have to be included).

If you are going to take advantage of Wscript classes (such as (like Wscript.Network or Wscript.Shell) in your scripts, add a reference for Windows Script Host Object Model and use it like the locator (for example,

Dim X as New IWshNetwork_Class

If you created the program in Visual Basic and want to translate it to VBScript (so that it can be used wherever scripts are used), use the following procedure.

To translate scripts developed as Visual Basic programs to VBScript

  1. Copy the subroutines from Visual Basic.

  2. Change the line from step 4 in the previous procedure to

    Dim loc
  3. Add

    Set loc=CreateObject("WbemScripting.SWbemLocator")
  4. The line that was added in step 5 of the previous procedure stays the same.

  5. Change lines that were added in step 6 to

    Dim <name>
    (and do not specify a data type).
  6. Where appropriate, translate any logic that outputs values (by adding them to list boxes, for example) to use Wscript.Echo instead.

  7. Where appropriate, add lines to create any objects that your Visual Basic program references, such as:

    Dim x As WshNetwork
    Set x = New WshNetwork

    Must be changed to:

    Set x=CreateOject("Wscript.Network"
For More Information

Did you find this information useful? Please send your suggestions and comments about the documentation to