Working with Objects and Namespaces


Let’s go back to the idea of a namespace introduced earlier in this chapter. You can think of a namespace as a way to organize or collect data related to similar items. Visualize an old-fashioned filing cabinet. Each drawer can represent a particular namespace. Inside this drawer are hanging folders that collect information related to a subset of what the drawer actually holds. For example, at home in my filing cabinet, I have a drawer reserved for information related to my woodworking tools. Inside this particular drawer are hanging folders for my table saw, my planer, my joiner, my dust collector, and so on. In the folder for the table saw is information about the motor, the blades, and the various accessories I purchased for the saw (such as an over-arm blade guard).

The WMI namespace is organized in a similar fashion. The NameSpaces are the file cabinets. The Providers are drawers in the file cabinet. The Folders in the drawers of the file cabinet are the WMI classes. These namespaces are shown in Figure 5-1.

image from book
Figure 5-1: WMI namespaces on Windows XP

Namespaces contain objects, and these objects contain properties you can manipulate. Let’s use a WMI script, image from book ListWmiNameSpaces.ps1, to illustrate just how the WMI namespace is organized. In the image from book ListWmiNameSpaces.ps1 script, the object returned from the WMI query is contained in the variable $wmi. The Get-WmiObject cmdlet is used to make the connection into the WMI. The class argument is used to specify the class of __Namespace, and the namespace argument is used to specify the the level in the WMI namespace hierarchy. The Get-WmiObject line of code is shown here:

 $wmi = Get-WmiObject ∈class __Namespace -namespace root

Tip 

When the Get-WmiObject cmdlet is run, it returns an object that is an array. We use this to our advantage on the second line of the image from book ListWmiNameSpaces.ps1 script. To easily get the name of the computer the script is run on, we print out the name of the __server property. But because $wmi contains an array, we cannot print out the value directly. We specify element 0 (the first element in the array) by supplying the [0] parameter to the variable. This section of code is shown here:

 "Listing namespaces on " + $wmi[0].__server +    " please wait a second "

Because all the namespaces are on the same computer, it really does not matter from which element we retrieve the data. All arrays will always have an element 0 populated if they contain any data at all.

The next section of code in the image from book ListWmiNameSpaces.ps1 script displays the progress indicator, which is a listing of dots, one for each namespace enumerated. If simply printing out the progress indicator dots would work, then this section of code would be rather easy; however, it becomes more complex because, by default, everything seems to print each item on a new line. To print out one dot for each item WMI namespace, we need to know how many WMI namespaces there are. This is where Windows PowerShell really shines. Because the object returned was an array, we need to know only how many items are in the array. We can obtain this information by obtaining the value for the length property of the $wmi variable. The for statement uses $i as the counter-variable, and will continue until the value of $i is less than or equal to the length of the $wmi array. The last section of the for statement increments the value of $i by 1. This section of the code is shown here:

 for ($i=0;$i -le $wmi.length;$i++)

To keep the Windows PowerShell prompt from tangling up with the output from our image from book ListWmiNameSpaces.ps1 script, we add an if else clause. If the dot does not represent the last namespace on the machine, we print each dot on the same line. We are able to do this by specifying the noNewLine argument from the Write-Host cmdlet. If the dot will represent the last namespace, then we want the newline character to be appended. To make the output a little more dramatic, we use the Start-Sleep cmdlet and specify an interval of 75 to the millisecond argument. This section of code is shown here:

 for ($i=0;$i -le $wmi.length;$i++)    {if ($i -lt $wmi.length)       {Write-Host -noNewLine "."       Start-Sleep -m 75}    else       {Write-Host "."}    }

The output section of the script pipelines the contents of the $wmi variable into the Format-List cmdlet and chooses only the name property. We then use the Write-Host cmdlet and supply the value of green for the foregroundcolor argument. We print out the length of the array holding all the WMI namespaces, using the grave accent character (`) to continue our line of code to the next line. At the end of the last line of code, we use the `n escape sequence to force a new line. The output section of code is shown here:

 $wmi | Format-List name     Write-Host -foregroundColor green "There are" $wmi.length `    "namespaces on this machine `n"

You will want to use the image from book ListWmiNameSpaces.ps1 script to get an idea of the number and variety of WMI namespaces that exist on the computer. This can be a great way to explore and to learn about WMI. The entire contents of the image from book ListWmiNameSpaces.ps1 script is shown here:

image from book ListWmiNameSpaces.ps1

 $wmi = Get-WmiObject ∈class __Namespace -namespace root    "Listing namespaces on " + $wmi[0].__server +    " please wait a second " for ($i=0;$i -le $wmi.length;$i++)    {if ($i -lt $wmi.length)       {Write-Host -noNewLine "."       Start-Sleep -m 75}    else       {Write-Host "."}    } $wmi | Format-List name    Write-Host -foregroundColor green "There are" $wmi.length `    "namespaces on this machine `n"

The output from the image from book ListWmiNameSpaces.ps1 script is shown here:

 Listing namespaces on MRED1 please wait a second .................. name : ServiceModel name : SECURITY name : MSAPPS12 name : CCM name : RSOP name : Cli name : aspnet name : SecurityCenter name : WMI name : CIMV2 name : Policy name : SmsDm name : Microsoft name : DEFAULT name : directory name : subscription name : MSAPPS11 There are 17 namespaces on this machine

So what does all this mean? It means that in Windows XP, there are more than a dozen different namespaces from which you could pull information about our server. Understanding that the different namespaces exist is the first step to being able to navigate WMI to find the information you need. Often, students and others new to PowerShell or VBScript work on a WMI script to make the script perform a certain action, which is a great way to learn scripting. However, what they often do not know is which namespace they need to connect to so that they can accomplish their task. When I tell them which namespace to work with, they sometimes reply, “That’s fine for you, but how do I know that the such-and-such namespace even exists?” By using the image from book ListWmiNameSpaces.ps1 script, you can easily generate a list of namespaces installed on a particular machine, and armed with that information, search on MSDN, http://msdn.microsoft.com/library/default.asp, to see what information it is able to provide.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

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