Selective Data from All Instances


The next level of sophistication (from using Select *) is to return only the properties you are interested in. This is a more efficient strategy. For instance, in the previous example, you entered Select * and were returned a lot of data you weren’t necessarily interested in. Suppose you want to know only what shares are on each machine.

image from book
Just the Steps

To select specific data

  1. Make a connection to WMI by using the Get-WmiObject cmdlet

  2. Use the query argument to supply the WMI query to the Get-WmiObject cmdlet

  3. In the query, use the Select statement to choose the specific property you are interested in, for example, Select name.

  4. In the query, use the From statement to indicate the class from which you want to retrieve data, for example, From Win32_Share.

image from book

Only two small changes in the image from book ListShares.ps1 script are required to enable garnering specific data through the WMI script. In place of the asterisk in the Select statement assigned at the beginning of the script, substitute the property you want. In this case, only the name of the shares is required.

The second change is to eliminate all unwanted properties from the Output section. The strange thing here is the way that PowerShell works. In the Select statement, we selected only the name property. However, if we were to print out the results without further refinement, we would retrieve unwanted system properties as well. By using the Format-List cmdlet and selecting only the property name, we eliminate the unwanted excess. Here is the modified image from book ListNameOnlyShares.ps1 script:

image from book ListNameOnlyShares.Ps1

 $strComputer = "." $wmiNS = "root\cimv2" $wmiQuery = "Select name from win32_Share" $objWMIServices = Get-WmiObject -computer $strComputer -namespace $wmiNS `    -query $wmiQuery $objWMIServices | Sort-Object -property name | Format-List -property name




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