Export (0) Print
Expand All

Calling Methods

Microsoft® Windows® 2000 Scripting Guide

Methods enable a script to carry out actions. For example, the Win32_Service class includes methods that let you perform such tasks as starting and stopping services; the Win32_NTEventLogfile class includes methods for backing up and clearing event logs; the Win32_OperatingSystem class includes methods for shutting down or rebooting a computer.

Listing 6.26 provides a template that can be used to write scripts that call WMI methods. This particular script uses the StopService method of the Win32_Service class to stop the Alerter service on the local computer.

Listing 6.26 Template for Calling Methods

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14


strComputer = "."
strNamespace = "\root\cimv2"
strClassName = "Win32_Service"
strKeyName = "Name"
strKeyValue = "Alerter"

Set objSWbemServices = GetObject("winmgmts:" &_
 "{impersonationLevel=impersonate}!\\" & strComputer & strNamespace)
Set colInstances = objSWbemServices.ExecQuery _
 ("SELECT * FROM " & strClassName & " WHERE " & strKeyName & " = '" &_
 strKeyValue & "'")
For Each objInstance in colInstances
 objInstance.StopService()
Next

To use this template with other WMI classes:

  1. Set the value of strClassName to the appropriate WMI class.

  2. If necessary, set the value of strNamespace to the appropriate WMI namespace.

  3. Set the value of strKeyName to the name of the property that forms the basis of the WHERE clause.

  4. Set the value of strKeyValue to the appropriate value for this property.

  5. Replace the statement within the For Each loop that calls the method. Remove the following line, and replace it with the appropriate line of code for the method being called. If necessary, you must also include the appropriate method parameters.

    
    
    objInstance.StopService()
    
    
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft