Connecting to WMI

The first thing that your scripts will have to do is connect to WMI, which is shown in Sample C.2.

Sample C.2 Connect.vbs - the part of a script that connects to WMI

Dim loc
Set loc = CreateObject( "WbemScripting.SWbemLocator" )
Dim WbemServices
Set WbemServices = loc.ConnectServer( ,"root\SMS\site_B2K" )

Only the fourth line ever changes. The B2K value is the site code of a primary site and it varies depending in which site you are running your script.

When the fourth line includes only the second parameter ("root\SMS\site_<sitecode>"), the script is intended to run on the site server. WMI does not accept any other parameters on that line when the script is run on the site server.

If you want to run your script from another computer (an SMS Administrator console, for example), your script still has to connect to the site server. However, to do that, the fourth line has to include a server name as its first parameter, as follows:

Set WbemServices = loc.ConnectServer( "servername", "root\SMS\site_B2K")

When connecting remotely, you can also specify a user name and password, as follows:

Set WbemServices = loc.ConnectServer( "servername, "root\SMS\site_B2K", "username", "password")

If you need to specify a domain for the user name, the line changes as follows:

Set WbemServices = loc.ConnectServer( "servername, "root\SMS\site_B2K", "domain\username",
"password")

Because the lines in Sample C.2 are used in all scripts in this appendix (except where otherwise noted) and they do not vary dramatically, some of the scripts in this appendix do not include these lines.

WMI supports multiple models for connecting to WMI from scripts. These models include the moniker and moniker session models, which allow you to connect to WMI by using fewer lines of code than the model discussed in this section. However, because these models are less flexible, most examples in this appendix do not use them. For more information about those models, see the WMI SDK.

For more advanced connection code that eliminates the need for referencing a site code, see the Microsoft Systems Management Server 2003 Software Development Kit.

For More Information

Did you find this information useful? Please send your suggestions and comments about the documentation to smsdocs@microsoft.com.