Selecting Multiple Properties
If you're interested in only a certain number of properties, you can use Select to specify that. All you have to do is separate the properties by a comma. Suppose you run the preceding script and find a number of undocumented shares on one of the serversyou might want a little bit more information such as the path to the share and how many people are allowed to connect to it. By default, when a share is created, the "maximum allowed" bit is set. This basically says anyone who has rights to the share can connect. This can be a problem, because if too many people connect to a share, they can degrade the performance of the server. To preclude such an eventuality, I always specify a maximum number of connections to the server.
Note  | I occasionally see people asking whether spaces or namecase in the property list matters. In fact, when I first started writing scripts and they failed, I often modified spacing and capitalization in feeble attempts to make the script work. Spacing and capitalization do not matter for WMI properties. |
The revised script, called ListName_Path_Max_Shares.vbs, now looks like the following:
ListName_Path_Max_Shares.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 path, allowMaximum from win32_Share"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)
For Each objItem in colItems
WScript.Echo "Name: " & objItem.Name
WScript.Echo "Path: " & objItem.path
WScript.Echo "AllowMaximum: " & objItem.AllowMaximum
WScript.Echo vbNewLine
Next
The technique of specifying only the properties you're interested in can be used with any of the supported properties from Win32_Share listed in Table 10-1. Interestingly enough, you don't really need to include the Name property on the Select line, because for Win32_Share, Name is the key property. This is seen in Figure 10-1.
Note  | The WMI CIM Studio tool is included with the WMI Admin Tools. These can be installed from \My Documents\Microsoft Press\VBScriptSBS\Resources\WMITools.exe. |
The key property in WMI works just like the key column in a database: It is used to uniquely identify a row, and it is often the column or property that is indexed to make searching easier. This is just like the key to a house or to a car. The key provides entry into the house or car so that you can access the property inside. The key property is always returned, even when it isn't specifically mentioned on the Select line.
To list running processes
1. | Open the \My Documents\Microsoft Press\VBScriptSBS\Templates\wmiTemplate.vbs script in Microsoft Notepad or some other script editor and save it as YourNameListRunningProcesses.vbs.
| 2. | Declare two new variables, strProperties and strValues. One will be used to hold the specific list of properties you will request. The other will hold the values that come back from the query. The code to do this looks like the following:
Dim strProperties 'properties to choose
Dim strValues 'string of wmi values
| 3. | Look up WIN32_process in the Platform SDK and choose some interesting properties. Assign them to the strProperties variable as a string. My code to do this looks like the following:
strProperties = "name,processID,pageFaults,WorkingSetSize"
| 4. | Modify the wmiQuery variable so that it selects only the properties specified in the strProperties variable from WIN32_process, but only if the process is not equal to 0, which is the system idle process. The code to do this looks like the following:
wmiQuery = "Select " & strProperties & " from win32_process" &_
" where processID <>0"
| 5. | Inside the For Each...Next loop, delete all the WScript.Echo lines. The new For Each...Next loop looks like the following:
For Each objItem in colItems
Next
| 6. | To reduce typing, you can use With and End With to allow us to refer to a series of properties on the object objItem without having to re-qualify the name objItem inside the For Each...Next loop. This means that every property will now use the qualifier objItem. This is seen below:
| 7. | Use the strValues variable to hold the output from retrieving the values from each instance in the collection. We are producing a csv output, and therefore we will place commas between each property value. The completed code is seen below:
strValues = .name & "," & .processID & "," &_
.PageFaults & "," & .WorkingSetSize
| 8. | After the End With statement, but before the Next command, print out the value of strValues by using WScript.Echo. This will be a debug statement, and later we will turn it off. The command looks like the following:
| 9. | Save and run your script in CScript. The output will look something like the (trimmed) output listed below. If your script does not work properly, compare it to the \My Documents\Microsoft Press\VBScriptSBS\ch10\ListRunningProcesses.vbs script.
System,4,11275,225280
smss.exe,732,211,380928
csrss.exe,932,42425,4616192
winlogon.exe,956,16377,4464640
|
Adding a logging subroutine
1. | Open the \My Documents\Microsoft Press\VBScriptSBS\ch10\ListRunningProcesses.vbs script and save it as YourNameLoggedRunningProcesses.vbs.
| 2. | Add a subroutine to be used to write out to a log file. This subroutine will accept one input parameter called strIN. The code to do this looks like the following:
Sub subText(strIN)
End sub
| 3. | At the top of the subroutine, add variables to hold filesystemobject, the file object, the wshShell object, and the path to the desktop and the name of the log file. I used the variables listed below.
Dim objFSO 'the filesystemobject
Dim objFile 'file object
Dim objShell 'wshshell object
Dim strPath 'path to desktop
Dim strFile 'log file name
| 4. | Define a constant ForAppending and set it to 8. Define an additional constant called createFile, and set it equal to True. This is seen below:
Const ForAppending = 8
Const createFile = True
| 5. | Below the subText subroutine, define a function called funfix that uses an input called strIN. Use this function to add a backslash ("\") to the beginning of the strIN parameter. Assign it back to the funfix function, as seen below:
Function funfix (strIN)
funfix = "\" & strIN
End Function
| 6. | Under the createFile constant in the subText subroutine, use the file name logProps.csv and assign it to the strFile variable. Use the funfix function to prepend a backslash to the file name. This is seen below.
strFile = funfix("logProps.csv")
| 7. | Create an instance of the wshShell object and assign the object that comes back from the CreateObject command to the variable objShell, as seen below:
Set objShell = CreateObject("WScript.Shell")
| 8. | Create an instance of FileSystemObject and assign the object that is returned to the objFSO variable, as seen below:
Set objFSO = CreateObject("Scripting.FileSystemObject")
| 9. | Use the SpecialFolders property of the wshShell object to retrieve the path to the desktop. Assign the path to the strPath variable. The code to do this is seen below:
strPath = objShell.SpecialFolders("desktop")
| 10. | Use the strPath variable to build up a complete path to the log file. Use strPath and concatenate it with the strFile variable, as seen below:
strFile = strPath & strFile
| 11. | Use the OpenTextFile method from the file system object to open the strFile file and append to it. If the file does not exist, create the file. Assign testStreamObject, which is returned to the objFile variable, as seen below:
Set objFile = objFSO.OpenTextFile(strFile,ForAppending,createFile)
| 12. | Once you have a text stream object, you can use the WriteLine method to write the data contained in the strIN variable to the file, as seen below:
objFile.WriteLine (strIN)
| 13. | In the main script, just under the line that defines the WMI query, call the subText subroutine, and pass it the contents of the strProperties variable. This will add a column header to the text output. The code to do this is seen below:
| 14. | In the main script, just under the end with statement, call the subText subroutine and pass the contents of the strValues variable. This code is seen below:
| 15. | The output will be a .csv file, which when double-clicked will open in Microsoft Excel if you have the Excel application installed. If you do have Excel installed, then the output will look like Figure 10-2.

| 16. | Save and run the script. LogProps.csv should be created on the desktop. If it is not, compare your script with \My Documents\Microsoft Press\VBScriptSBS\ch10\LoggedRunningProcesses.vbs.
|
 |
Q. | To select specific properties from an object, what do you need to do on the Select line?
| A. | You need to separate the specific properties of an object with a comma on the Select line of the ExecQuery method. | Q. | To avoid error messages, what must be done when selecting individual properties on the Select line?
| A. | Errors can be avoided if you make sure each property used is specified in the Select line. For example, the WMI query is just like a paper bag that gets filled with items that are picked up by using the Select statement. If you do not put something in the paper bag, you cannot pull anything out of the bag. In the same manner, if you do not "select" a property, you cannot later print or sort on that property. This is exactly the way that a SQL Select statement works. | Q. | What can you check for in your script if it fails with an "object does not support this method or property" error?
| A. | If you are getting "object does not support this method or property" error messages, you might want to ensure you have referenced the property in your Select statement prior to trying to work with it in an Output section. |
|
|
 |