Previous Page
Next Page

Digging Deeper

Knowing the default namespaces gives some information, and though it's helpful, to better map out the WMI namespace, you'll want information about the child namespaces as well. You'll need to implement a recursive query so that you can gain access to the child namespace data. The next script, RecursiveListWMINamespaces.vbs, is similar to ListWMI Namespaces.vbs, except that it uses a subroutine that calls itself to list the child namespaces. On some computers, this script might seem to perform a little slowly during the first running, so I included a WScript.Echo (Now) command at the beginning and at the end of the script. This enables the network administrator to determine how long the script takes to run.

As with the previous script, RecursiveListWMINamespaces.vbs uses strNamespace with a "." to indicate the script is run against the local computer. It then calls the subroutine named EnumNamespaces and starts with the "root" namespace.

Subroutines

Basically, a subroutine is a section of a script that you can get to from anywhere inside the script. All we need to do is call the subroutine by name to jump to a particular part of the script. You use a subroutine in this script rather than code that is sequential because you need to execute the commands that make up the subroutine as a group. When you are finished, you exit out. You can easily identify a subroutine because it begins with the word Sub followed by the name of the subroutine, and it ends with the End sub command. When you exit a subroutine (via the End sub command), you go back to the line after the one that caused you to enter the subroutine.


Once you enter the subroutine, you echo strNamespace, which on the first pass is simply the root. Next you use GetObject to make a connection to the WMI namespace that is identified by the subroutine strNamespace argument. In the first pass, you are connected to the root. The subroutine then retrieves all namespaces that are immediately below the one it is currently connected to. You then use a For Each...Next construction to loop through all the namespaces below the currently connected one. In doing so, you also concatenate the names to provide a fully qualified name to the namespace. You take the newly constructed name, pass it to EnumNamespaces, and work through the namespace one more time.

RecursiveListWMINamespaces.vbs

WScript.Echo(Now)
strComputer = "."
Call EnumNamespaces("root")

Sub EnumNamespaces(strNamespace)
    WScript.Echo strNamespace
    Set objSWbemServices = _
        GetObject("winmgmts:\\" & strComputer & "\" & strNamespace)
    Set colNamespaces = objSWbemServices.InstancesOf("__NAMESPACE")
    For Each objNameSpace In colNamespaces
        Call EnumNamespaces(strNamespace & "\" & objNamespace.Name)
    Next
End sub
WScript.Echo("all done " & Now)


Previous Page
Next Page