Previous Page
Next Page

One Step Further: Using Win32_Environment and VBScript to Learn About WMI

In this section, you use Win32_Environment and VBScript to learn about both WMI and the environment settings on your server.

1.
Open Notead or some other script editor.

2.
On the first line, type Option Explicit.

3.
Use the Dim command to declare the following variables: objWMIService, colItems, objItem, wmiQuery, and strComputer.

4.
Use WScript.Echo and the Now function to indicate the script is beginning its run.

5.
Assign the value of "." to the variable strComputer.

6.
Assign the query "Select * from Win32_Environment" to the variable wmiQuery.

7.
Set objWMIService equal to the handle that comes back from the GetObject function with the winmgmts: moniker. Incorporate the variable strComputer to tell WMI which computer to use to execute the connection.

8.
Use the colItems variable to hold the object returned from using the execQuery method of the SWbemServicesEx object. This is seen below:

Set colItems = objWMIService.ExecQuery(wmiQuery)

9.
Use a For Each...Next loop to iterate through the collection called colItems. For each objItem in colItems, echo out the following properties: Caption, Description, InstallDate, Name, Status, SystemVariable, UserName, and VariableValue.

10.
Close out the For Each...Next loop.

11.
Echo a line indicating the script is finished and use the Now function to print out the time.

12.
Save your work as SysEnvironment.vbs.

13.
Run the script in CScript.

Chapter 9 Quick Reference

To

Do This

Simplify connecting into WMI, while using default security permissions

Use the WMI moniker

Control security when making a remote connection

Specify the impersonation levels in your script

Allow a script to use the credentials of the person launching the script

Use the Impersonate impersonation level

Allow a script to shut down the server

Use the shutdown privilege



Previous Page
Next Page