• Aucun résultat trouvé

In most situations, when you use WMI, you are performing some sort of query. Even when you’re going to set a particular property, you still need to execute a query to return a dataset that enables you to perform the configuration. (A dataset is the data that comes back to you as the result of a query, that is, it is a set of data.) In this sec€

tion, you’ll look at the methods used to query WMI.

Just the Steps

To query WMI

1. Specify the computer name.

2. Define the namespace.

3. Connect to the provider using GetObject.

4. Issue the query.

5. Use For Each...Next to iterate through collection data.

One of the problems with Windows Server 2003 for the small to medium enterprise is Windows Server 2003 product activation. Although the larger customers have the advantage of “select” keys that automatically activate the product, smaller companies often are not aware of the advantages of volume licensing and as a result do not have access to these keys. In addition, I’ve seen larger customers that use the wrong key—

you can easily forget to activate the copy of Windows Server 2003. Many customers like to monitor the newly built machine prior to actual activation because of the problems resulting from multiple activation requests. As is often the case with many IT depart€

ments, emergencies arise, and it is easy to forget to make the trek back to the server rooms to activate the machines. This is where the power of WMI scripting can come to the rescue. The following script uses the new Win32_WindowsProductActivation WMI class to determine the status of product activation:

Option Explicit On Error Resume Next dim strComputer dim wmiNS dim wmiQuery dim objWMIService dim colItems Dim objItem strComputer = “."

wmiNS = “\root\cimv2"

wmiQuery = “Select * from Win32_WindowsProductActivation"

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & wmiNS) Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem In colItems

WScript.Echo “ActivationRequired: “ & objItem.ActivationRequired WScript.Echo “IsNotificationOn: “ & objItem.IsNotificationOn WScript.Echo “ProductID: “ & objItem.ProductID

WScript.Echo “RemainingEvaluationPeriod: “ & _ objItem.RemainingEvaluationPeriod

WScript.Echo “RemainingGracePeriod: “ & objItem.RemainingGracePeriod WScript.Echo “ServerName: “ & objItem.ServerName

Next

Header Information

The Header information section of DisplayWPAstatus.vbs contains the two normal items, Option Explicit and On Error Resume Next. (If you are unfamiliar with these commands, refer to Chapter 1, “Starting from Scratch.”) Next, you declare six variables to be used in this script. Because you are writing a WMI script, you make up some new variable names. Table 8-1 lists the variables and their intended use in this script.

Table 8-1 Variables Used in DisplayWPAstatus.vbs Variable name Variable use

strComputer Holds the name of the computer the query will target at run time wmiNS Holds the namespace that the WMI query will target

wmiQuery Holds the WMI query

objWMIService Holds the connection to the WMI service

colItems Holds the collection of items returned by the WMI query

objItem Holds the individual item from which the properties will be queried

Reference Information

The Reference information section of the script is used to assign value to some of the variables declared in the Header information section. The first variable used in the Ref€

erence information section is strComputer, whose value is set to ".". In WMI shorthand,

"." is used to mean “this computer only.” So the WMI query will operate on localhost.

The second variable assigned a value is wmiNS, which is used to hold the value of the WMI namespace you query. You could include the namespace and the query on the same line of the script; however, by breaking the namespace and the query out of the connection string, you make it easier to reuse the script. WmiQuery is the next vari€

able, which receives the value of “Select * from Win32_WindowsProductActivation.”

You can easily change the query to ask for other information. You are asking for every-thing that is contained in the local computer from the Win32_WindowsProduct-Activation namespace.

You use the Set command to set objWMIService to the handle that is obtained by the GetObject command. The syntax for this command is very important because it is sem€

inal to working with WMI. When making a connection using winmgmts://, the use of winmgmts is called a moniker. A moniker works in the same way that the phrase “abra€

cadabra” used to work in the old movies. It’s a shortcut that performs a lot of connec€

tion work in the background. Remember the magic phrase, because it will do much of the work for you, including opening the door to the storehouse of valuable WMI data.

The last item in the Reference information section is the assignment of the variable colItems to the handle returned by the ExecQuery method. The Reference information section follows:

strComputer = “."

wmiNS = “\root\cimv2"

wmiQuery = “Select * from Win32_WindowsProductActivation"

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & wmiNS) Set colItems = objWMIService.ExecQuery(wmiQuery)

Worker and Output Information

The Worker information section is the part of the script that works through the collec€

tion of data returned and produces the WPA information. This section is always going to be customized for each WMI script you write, because each query or each provider used returns customized data.

Because WMI returns data in the form of a collection, you need to use a For Each...Next loop to iterate through the items in the collection. This loop is required—

even when WMI returns only one item, WMI still returns that item in a collection. Your question at this point is probably “how do I know what to request from WMI?” I looked that up in the Platform SDK (Software Development Kit). (The Platform SDK contains tons of detailed information about the operating system and is downloadable for free from Microsoft.com. In addition to selecting the core SDK, you should also download t h e W M I S D K f o r W i n d o w s S e r v e r 2 0 0 3 . ) B y l o o k i n g i n t h e S D K f o r Win32_WindowsProductActivation, you learn that several fields are available as prop€

erties from which you can return information. The SDK also tells you that the fields are

all read-only (which would prevent us from flipping the ActivationRequired field to false). The Worker and Output information section of this script follows:

For Each objItem In colItems

WScript.Echo “ActivationRequired: “ & objItem.ActivationRequired WScript.Echo “IsNotificationOn: “ & objItem.IsNotificationOn WScript.Echo “ProductID: “ & objItem.ProductID

WScript.Echo “RemainingEvaluationPeriod: “ & _ objItem.RemainingEvaluationPeriod

WScript.Echo “RemainingGracePeriod: “ & objItem.RemainingGracePeriod WScript.Echo “ServerName: “ & objItem.ServerName

Next

The most interesting information in Win32_WindowsProductActivation is listed in Table 8-2.

Table 8-2 Properties of Win32_WindowsProductActivation

Property Meaning

ActivationRequired4 If 0, activation is not required. If 1, the system must be acti€

vated within the number of days indicated by the Remaining3 GracePeriod property.

IsNotificationOn4 If 0, notification reminders and the activation icon are disabled.

If not equal to 0 and product activation is required, notification reminders (message balloons) are enabled, and the activation icon appears in the notification tray.

ProductID4 A string of 20 characters separated by hyphens. This is the same product ID that is displayed on the General tab of the System Properties dialog box in Control Panel.

RemainingEvaluationPeriod4 If beta or evaluation media, this returns the number of days remaining before expiration. If retail media, this field is set to the largest possible unsigned value.

RemainingGracePeriod4 Numbers of days remaining before activation is required if ActivationRequired is equal to 1.

ServerName4 Name of the system being queried. This could also be the IP address of the system.

Summary

In this chapter, you looked at WMI on Windows Server 2003. You learned about the concept of the namespace and examined several of the default namespaces available in a default installation of Windows Server 2003. In addition, you looked at the concept of classes and how they are utilized within WMI. You examined the concept of provid€

ers and saw how they are used to enable WMI to access various parts of the informa€

tion contained within the different namespaces. You learned about querying WMI and about the use of monikers to abstract some of the complexity of connecting to WMI.

Quiz Yourself

Q. What is the default WMI namespace on Windows Server 2003?

A. The default WMI namespace on Windows Server 2003 actually depends on how you define default. Many of the WMI tools will connect to root\cimv2, which contains a lot of very useful information for managing Windows Server 2003. You could also say that root is the default namespace as well because it is at the top of the tree.

Q. You want to find a class in WMI that will tell you how much memory is installed on a server. How do you go about finding this class?

A. There are several approaches to this task. You could download the WMI SDK, which includes the WMI browser. After you launch the WMI browser, you could look around and see what you find. You could also do a search in the WMI SDK for the term “WMI memory,” which would return the Win32_PhysicalMemory class, and from this class you could return several pieces of information about installed memory.

Q. Why do many scripts set the computer variable equal to "."?

A. When a script sets the computer variable equal to ".", the writer wants the script to run against the local machine.

Q. What is a moniker?

A. A moniker is a connection shortcut that hides much of the complexity of connecting to WMI from the scripter.

On Your Own

Lab 16 Retrieving Hotfix Information

In this lab, you use the Win32_QuickFixEngineering provider to retrieve information about hotfixes installed on your server. This lab incorporates techniques learned in ear€

lier chapters into the information about WMI discussed in this chapter.

Lab Instructions

1. Open Notepad.exe.

2. Turn on Option Explicit by typing Option Explicit on the first line of the script.

3. Declare variables to be used in the script. There are six variables to be used: str-Computer, objWmiService, wmiNS, wmiQuery, objItem, and colItems.

4. Assign the value of "." to the variable strComputer. The code will look like the fol€

lowing:

strComputer = “.”

5. Assign the value of "\root\cimv2" to the variable wmiNS. The code will look like the following:

wmiNS = “\root\cimv2”

6. Assign the query "Select * from Win32_QuickFixEngineering" to the variable wmiQuery. The code will look like the following:

wmiQuery = “Select * from Win32_QuickFixEngineering”

7. Use the winmgmts moniker and the variable objWMIService as well as the Get-Object method to make a connection to WMI. Use the strComputer and the wmiNS variables to specify the computer and the namespace to use. The code will look like the following:

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & wmiNS)

8. Set the variable colItems to be equal to the connection that comes back from WMI when it executes the query defined by wmiQuery. Your code should look like the following:

Set colItems = objWMIService.ExecQuery(wmiQuery)

9. Use a For Each...Next construction to iterate through the collection called colItems.

Assign the variable called objItem to each of the items returned from colItems.

Your code should look like this:

For Each objItem In colItems

10. Use WScript.Echo to echo out items such as the caption, CSName, and description.

Yo u c a n c o p y t h e f o l l o w i n g i t e m s , o r u s e t h e W M I S D K t o l o o k u p Win32_QuickFixEngineering and choose items of interest to you.

WScript.Echo “Caption: “ & objItem.Caption WScript.Echo “CSName: “ & objItem.CSName

WScript.Echo “Description: “ & objItem.Description WScript.Echo “FixComments: “ & objItem.FixComments WScript.Echo “HotFixID: “ & objItem.HotFixID WScript.Echo “InstallDate: “ & objItem.InstallDate WScript.Echo “InstalledBy: “ & objItem.InstalledBy WScript.Echo “InstalledOn: “ & objItem.InstalledOn WScript.Echo “Name: “ & objItem.Name

WScript.Echo “ServicePackInEffect: “ & objItem.ServicePackInEffect WScript.Echo “Status: “ & objItem.Status

11. Close out your For Each...Next loop with the Next command.

12. Save your file as lab16.vbs.

Lab 17 Echoing the Time Zone

In this lab, you modify the script from Lab 16 so that it echoes out the time zone con-figured on the computer.

Lab Instructions

1. Open Notepad.exe.

2. Open Lab16Solution.vbs, and save it as lab17.vbs.

3. Edit the wmiQuery so that it points to Win32_TimeZone. The code will look like the following:

wmiQuery = “Select * from Win32_TimeZone”

4. Inside the For Each objItem In colItems loop, delete all but one of the WScript.Echo statements so that the code looks like the following:

For Each objItem In colItems

WScript.Echo “Caption: “ & objItem.Caption Next

5. Save and run the file. You are now pointing to the Caption field of Win32_TimeZone. No further changes are required for this lab.

9 WMI Continued

In this chapter, you’ll continue working with WMI. You’ll build upon the concepts learned in Chapter 8, “Why Windows Management Instrumentation?” and see different ways to leverage your investment in WMI to assist in day-to-day network administrative tasks.

Before You Begin

To work through the material presented in this chapter, you need to be familiar with the following concepts from earlier chapters:

Connecting to the default WMI namespace

Accessing properties of dynamic WMI classes

Implementing the For...Next construction

Implementing a WMI query

After completing this chapter you will be familiar with the following:

Alternative ways of configuring the WMI moniker

Querying WMI

Setting impersonation levels

Defining the WMI object path

Navigating the WMI namespace