Choosing Specific Instances


In many situations, you will want to limit the data you return to a specific instance of that class in the dataset. If you go back to your query and add a Where clause to Select statement, you’ll be able to greatly reduce the amount of information returned by the query. Notice that in the value associated with the wmiQuery, you added a dependency that indicated you wanted only information with share name C$. This value is not case sensitive, but it must be surrounded with single quotation marks, as you can see in the wmiQuery string in the following script. These single quotation marks are important because they tell WMI that the value is a string value and not some other programmatic item. Because the addition of the Where statement was the only thing you really added to the image from book ListShares.ps1 script, we do not provide a long discussion of the image from book ListSpecificShares.ps1 script.

image from book ListSpecificShares.ps1

 $strComputer = "." $wmiNS = "root\cimv2" $wmiQuery = "Select * from win32_share where name='c$'" $objWMIServices = Get-WmiObject -computer $strComputer -namespace $wmiNS    -query $wmiQuery $objWMIServices | Format-List *

image from book
Just the Steps

To limit specific data

  1. Make a connection to WMI.

  2. Use the Select statement in the WMIQuery argument to choose the specific property you are interested in, for example, Select name.

  3. Use the From statement in the WMIQuery argument to indicate the class from which you want to retrieve data, for example, From Win32_Share.

  4. Add a Where clause in the WMIQuery argument to further limit the dataset that is returned. Make sure the properties specified in the Where clause are first mentioned in the Select statement, for example, Where name.

  5. Add an evaluation operator. You can use the equals sign (=), or the less than (<) or greater than (>) symbols, for example, Where name = 'C$'

image from book

Eliminating the WMIQuery argument

  1. Open Notepad or some other Windows PowerShell script editor.

  2. Declare a variable called $strComputer and assign the WMI shortcut dot (.) to it. The shortcut dot means connect to the WMI service on the local computer. This command is shown here:

     $strComputer = "."

  3. Declare another variable and call it $wmiClass. Assign the string "WIN32_Share" to the variable. This code is shown here:

     $wmiClass = "win32_Share"

  4. Declare a variable and call it $wmiFilter. This variable will be used to hold the string that will contain the WMI filter to be used with the Get-WmiObject command. The variable and the associated string value are shown here:

     $wmiFilter = "name='c$'"

  5. Declare a variable called objWMIServices and assign the object that is returned from the Get-WmiObject cmdlet to the variable. Specify the computer argument and supply the value contained in the $strComputer variable to it. At the end of the line, use the grave accent character (`) to indicate line continuation. This line of code is shown here:

     $objWMIServices = Get-WmiObject -computer $strComputer `

  6. Use the class argument to supply the class name for the WMI query to the Get-WmiObject cmdlet. The class name to query is contained in the $wmiClass variable. On the same line, use the filter argument to supply the filter string contained in the $wmiFilter variable to the Get-WmiObject cmdlet. This line of code is shown here:

     -class $wmiClass -filter $wmiFilter

  7. On the next line, use the object contained in the $objWMIServices variable and pipeline it to the Format-List cmdlet. Use the asterisk to tell the Format-List cmdlet you wish to retrieve all properties. This line of code is shown here:

     $objWMIServices | Format-List *

  8. The completed script is shown here:

     $strComputer = "." $wmiClass = "win32_Share" $wmiFilter = "name='c$'" $objWMIServices = Get-WmiObject -computer $strComputer `    -class $wmiClass -filter $wmiFilter    $objWMIServices | Format-List *

  9. A sample output is shown here:

     Status           : OK Type             : 2147483648 Name             : C$ __GENUS          : 2 __CLASS          : Win32_Share __SUPERCLASS     : CIM_LogicalElement __DYNASTY        : CIM_ManagedSystemElement

  10. If your results are not similar, compare your script with the image from book ShareNoQuery.ps1 script.

  11. This completes the eliminating the WMIQuery argument procedure.




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