Using WMI to get a single value
If you go to the "WMI" node in the Engine Browser tree in the script editor, you will find only two sub-nodes.
One is the function that retrieves one WMI value and one is the collection that retrieves multiple values.
And essentially, this is all you need, because this is enough to get information from WMI.
If you are doing very advanced WMI scripting, you may need to work
on WMI objects, and this is described further down.
If we wanted to get the total memory installed, we would first have to find out in what WMI class we need, which
we can see here:
Computer System Hardware Classes.
In this case need the TotalPhysicalMemory from the Win32_ComputerSystem class. We know we just need to get one value
from WMI, so we can just use the WMIQuery function like this:
ShowMessage [WMIQuery Select TotalPhysicalMemory
From Win32_ComputerSystem]
And voila, we have the total memory. For demonstration purposes, we are just displaying the value on the screen.
In real life, the WMIQuery function could be used like this: "Set MyVariable = [WMIQuery Select TotalPhysicalMemoryFrom Win32_ComputerSystem]"
to save the information in a variable for later use.
But here is the one thing you must know, when using WMI with
FastTrack: If there is a native function to get the same information - use it. The
native version will in many cases give you this information in a few milliseconds, where the WMI
layer will take seconds to get the same information. Before you start to use WMI, try to search for a
native function with the F9 key (Search Engine Browser). Searching for "memory" will show us that
there is a function called "TotalMemory". So we can instead simply go:
ShowMessage
[TotalMemory]
Now there are a number of advantages using the native version. First of all, they are usually quicker
and secondly, they are often formatted. Where the WMI version will give you 4080713728 (bytes) on a 4 gigabyte
machine, the native version will give you a more useful 4080 (megabytes).
Using WMI to get multiple values
In some cases we would need more than one value from a query. In this case we need the
WMIValues collection. This could be relevant, if we wanted to collect all disk drives
in a computer. We could log this information to a file or store it elsewhere, but for
demonstration purposes, we will just show the retrieved names of the drives:
ForEach Model in [WMIValues SELECT model from Win32_DiskDrive]
ShowMessage [Var Model]
End ForEach
Using WMI to get multiple properties
If we need more than one property in a WMI query, we need to work on WMI objects
through COM. Refer to
this page
for documentation on using COM with FastTrack.
As shown in the article, we can create an instance of the Win32_LogicalDisk class
and go through the results. In the example below, we are displaying the property "Name" of
each disk object, but all other properties and methods on the Win32_LogicalDisk object can
be used through the ObjectValue function, as we used the star-notation (*).
ForEachObject Drive in CreateWMIObject("Select
* from Win32_LogicalDisk")
ShowMessage [ObjectValue Drive.Name]
End ForEachObject
If the example, we are using one property. If we need to invoke a method, we can
do this the same way we use properties. The best example on using methods is the
Hyper-V Controller script shown
here,
where the "RequestStateChange" method is invoked to stop and start Hyper-V machines.