Section B.2. Enumerating Objects of a Particular Class


B.2. Enumerating Objects of a Particular Class

Now let's enumerate all l ogical disks on a machine. To do so, we need to use the InstancesOf method. An example should make this clear:

strComputer = "." set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objDisks = objWMI.InstancesOf("Win32_LogicalDisk") for each objDisk in objDisks     Wscript.Echo "DeviceID: " &  objDisk.DeviceID         Wscript.Echo "FileSystem: " &  objDisk.FileSystem         Wscript.Echo "FreeSpace: " & objDisk.FreeSpace         Wscript.Echo "Name: " & objDisk.Name         Wscript.Echo "Size: " & objDisk.Size         WScript.Echo "" next

Here we get a WMI object which points to the root\CIMv2 namespace, after which we call the InstancesOf method and pass the Win32_LogicalDisk class. That method returns a collection of Win32_LogicalDisk objects, which we then iterate over with a for loop.

Since we used a for loop in the last example, we'll show the equivalent code in Perl:

use Win32::OLE 'in'; my $strComputer = "."; my $objWMI = Win32::OLE->GetObject("winmgmts:\\\\$strComputer\\root\\cimv2"); my $objDisks = $objWMI->InstancesOf("Win32_LogicalDisk"); for my $objDisk (in $objDisks) {     print "DeviceID: ", $objDisk->DeviceID,"\n";     print "FileSystem: ", $objDisk->FileSystem    ,"\n";     print "FreeSpace: ", $objDisk->FreeSpace,"\n";     print "Name: ", $objDisk->Name,"\n";         print "Size: ", $objDisk->Size,"\n";     print "\n"; }

As you can see, the Perl code is very similar to the VBScript code. One thing to note is that we had to import the in function on the first line because we use it later in the for loop to iterate over the $objDisks collection. VBScript provides this function natively within the language, whereas Perl does not.

Having the capability to easily obtain all instances of a certain type of class is very powerful. As you can imagine, you could adapt the code to retrieve a list of all CPUs, services, processes, etc., on a specific computer. The only issue with the last example is that prior to writing the script, we needed to know which property methods of the Win32_LogicalDisk class we wanted to print. We can instead retrieve all properties of the Win32_LogicalDisk class using the Properties_ method on each object as shown here:

strComputer = "." strWMIClass = "Win32_LogicalDisk" set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objDisks = objWMI.InstancesOf(strWMIClass) for each objDisk in objDisks    for each objProp in objDisk.Properties_       ' Print out NULL if the property is blank       if IsNull(objProp.Value) then          Wscript.Echo " " & objProp.Name & " : NULL"       else       ' If the value is an array, we need to iterate through each element       ' of the array          if objProp.IsArray = TRUE then             For I = LBound(objProp.Value) to UBound(objProp.Value)                wscript.echo " " & objProp.Name & " : " & objProp.Value(I)             next          else        ' If the property was not NULL or an array, we print it             wscript.echo " " & objProp.Name & " : " & objProp.Value          end if       end if     next    WScript.Echo "" next



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net