Tell Me Everything About Everything!When novices first write Windows Management Instrumentation (WMI) scripts, they nearly all begin by asking for every property about all instances of a class that are present on a particular system. (This is also referred to as the infamous "Select * query".) This approach can often return an overwhelming amount of data, particularly when you are querying a class such as installed software, or processes and threads. Rarely would one need to have so much data. Typically, when looking for installed software, you're looking for information about a particular software package. There are, however, several occasions when I want to use the "tell me everything about all instances of a particular class" query:
Just the Steps In the next script, you make a connection to the default namespace in WMI and return all the information about all the shares on a local machine. This is actually good practice, because in the past, numerous worms propagated via unsecured shares, and you might have unused shares arounda user might create a share for a friend and then forget to delete it. In the script that follows, called ListShares.vbs, all the information about shares present on the machine is reported. ListShares.vbs 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_Share" Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS) Set colItems = objWMIService.ExecQuery(wmiQuery) For Each objItem In colItems WScript.Echo "AccessMask: " & objItem.AccessMask WScript.Echo "AllowMaximum: " & objItem.AllowMaximum WScript.Echo "Caption: " & objItem.Caption WScript.Echo "Description: " & objItem.Description WScript.Echo "InstallDate: " & objItem.InstallDate WScript.Echo "MaximumAllowed: " & objItem.MaximumAllowed WScript.Echo "Name: " & objItem.Name WScript.Echo "Path: " & objItem.Path WScript.Echo "Status: " & objItem.Status WScript.Echo "Type: " & objItem.Type WScript.Echo Next Header InformationThe Header information section of ListShares.vbs contains all the standard information. Option Explicit forces the declaration of all variables. This is followed by On Error Resume Next to make sure the script goes to the next line of code if it encounters an error. Note
These two standard lines are followed by the same variable names declared in previous WMI scripts: strComputer, wmiNS, wmiQuery, objWMIService, colItems, and objItem. The variable strComputer defines the target computer, wmiNS specifies the target WMI namespace, wmiQuery holds the value of the query to be executed, and colItems holds the collection of items that are returned by the query. The variable objItem is used by the For Each...Next loop to iterate through the collection. Reference InformationThe Reference information section of the script is used to assign values to five of the six variables. The variable strComputer is assigned the value of ".", which indicates the script will run against the local computer. The variable wmiNS is assigned to \root\cimv2, which is the default WMI namespace. The variable wmiQuery is set to "Select * from Win32_Share". This is the query you want to execute against the default WMI namespace. Select * tells WMI that you want to retrieve all properties from the Win32_Share object. Note that this query doesn't display all the properties; it simply displays all the properties from the Win32_Share object. What you do with the returned data depends on your current needs. Unless you need it, returning all the data might not be a very efficient use of networking resources. It is, however, very easy to construct such a query. The variable objWMIService is used to connect to WMI, and it uses the WMI moniker to do so. Two variables assist in this operation: strComputer and wmiNS. The colItems variable holds the handle that comes back from the ExecQuery method that is used to execute your WMI query against the Win32_Share class. Worker and Output InformationThe Worker information and Output information sections of the ListShare.vbs script are combined, and the script simply uses WScript.Echo to write the various properties and their associated values to the command line (if run in CScript) or to a pop-up dialog box (if run in WScript, which is not a really good idea when you have numerous shares). The most convenient listing of all the available properties for a particular class is contained in the Platform SDK. A quick search for Win32_Share reveals the properties listed in Table 10-1.
|