Previous Page
Next Page

Including Additional Security Permissions

In this section, you will modify the WMI moniker to include the specification of additional security permissions. You will use a script that displays information about the display.

1.
Open Notepad or a different script editor.

2.
On the first line, type Option Explicit to ensure variables are declared and spelled correctly.

3.
On the next line, declare the following variables: objWMIService, colItems, and objItem. These are the same variables you used in previous scripts in this chapter.

4.
Set objWMIService equal to what comes back from the GetObject method when used in conjunction with the WMI moniker. In addition, you want to define an impersonation level of Impersonate as well as the special debug privilege. Your code will look like the following:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (debug)}")

5.
Set colItems equal to what comes back from issuing the WQL statement "Select * from Win32_DisplayConfiguration" as you use the ExecQuery method. Your code will look like the following:

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DisplayConfiguration")

Use a For Each...Next loop to iterate through colItems as you look for the following properties of the Win32_DisplayConfiguration object: BitsPerPel, Caption, Description, DeviceName, DisplayFlags, DisplayFrequency, DriverVersion, LogPixels, PelsHeight, PelsWidth, SettingID, and SpecificationVersion. Use the variable objItem to assist you in iterating through the collection. Make sure you close out the For Each...Next loop with the Next command. Your code will look like the following:

For Each objItem in colItems
  WScript.Echo "BitsPerPel: " & objItem.BitsPerPel
  WScript.Echo "Caption: " & objItem.Caption
  WScript.Echo "Description: " & objItem.Description
  WScript.Echo "DeviceName: " & objItem.DeviceName
  WScript.Echo "DisplayFlags: " & objItem.DisplayFlags
  WScript.Echo "DisplayFrequency: " & objItem.DisplayFrequency
  WScript.Echo "DriverVersion: " & objItem.DriverVersion
  WScript.Echo "LogPixels: " & objItem.LogPixels
  WScript.Echo "PelsHeight: " & objItem.PelsHeight
  WScript.Echo "PelsWidth: " & objItem.PelsWidth
  WScript.Echo "SettingID: " & objItem.SettingID
  WScript.Echo "SpecificationVersion: " & objItem.SpecificationVersion
Next

6.
Save your program as Display.vbs.

7.
Modify the WMI connection string to include not only the debug privilege, but also the shutdown privilege. Your code will look like the following:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate, (debug, shutdown) }")

8.
Modify the WMI connection string to indicate that the WMI connection should attach to the local host machine. This WMI connection string is starting to be rather long, so break the line after you specify the impersonation level. Your code will look like the following:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate," _
& "(debug, shutdown)}\\localhost")

9.
Save your work.

10.
Modify the connection in the preceding string to indicate that you want WMI to make a connection to the \root\cimv2 namespace on the computer called localhost. Your code will look like the following:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate," _
  & "(debug, shutdown)}\\localhost\root\cimV2")

11.
Save your work and then use CScript to run the script.


Previous Page
Next Page