Querying WMI


In most situations, when you use WMI, you are performing some sort of query. Even when you’re going to set a particular property, you still need to execute a query to return a dataset that enables you to perform the configuration. (A dataset includes the data that come back to you as the result of a query, that is, it is a set of data.) In this section, you’ll look at the methods used to query WMI.

image from book
Just the Steps

To query WMI:

  1. Specify the computer name.

  2. Define the namespace.

  3. Connect to WMI by using Get-WMIObject.

  4. Specify the query by supplying a value for the filter argument or the query argument.

  5. Use Format-List or another cmdlet to clean up the output.

image from book

In the image from book QueryDesktop.ps1 script, we define three variables that are used to hold the arguments for the Get-WmiObject cmdlet. These variables are $wmiQuery, which holds the WMI query; $wmiNS, which holds the WMI namespace that contains the WIN32_Desktop WMI class; and $strComputer, which holds the name of the computer we wish to query. In this case, we are using the shortcut dot (.), which means we are going to query only the local computer. We could have left out both the WMI namespace and the computer name if we wanted to rely on the default computer configuration. The $wmiQuery contains the WMI query, which is written in the WMI query language (WQL). WQL is a subset of Transact SQL (T-SQL), with special modifications to make it more appropriate for working with WMI. The line of code that contains the WMI query is shown here:

 $wmiQuery = "Select * from Win32_Desktop"

The WMI class WIN32_Desktop represents the properties and user-defined configuration settings of the standard Windows desktop. The screensaver timeout value and the secure screensaver properties are particularly important. The $objWMIService variable is used to hold the object returned by the Get-WmiObject cmdlet. This object is then pipelined to the Format-List cmdlet. These two lines of code are shown here:

 $objWMIService = Get-WmiObject -computer $strComputer -namespace $wmiNS -query $wmiQuery $objWMIService | Format-List *

The completed image from book QueryDeskTop.ps1 script is shown here:

image from book QueryDesktop.ps1

 $wmiQuery = "Select * from Win32_Desktop" $wmiNS = "root\cimv2" $strComputer = "." $objWMIService = Get-WmiObject -computer $strComputer -namespace $wmiNS -query $wmiQuery $objWMIService | Format-List *

Modifying the QueryDesktop.ps1 script

  1. Open the image from book QueryDesktop.ps1 script in Notepad.exe or your favorite script editor. Save the script as yournameimage from book ModifiedDesktop.ps1.

  2. Run the yournameimage from book ModifiedDesktop.ps1 script. It should run without errors and will produce an output that is similar to the one shown here:

     __GENUS               : 2 __CLASS               : Win32_Desktop __SUPERCLASS          : CIM_Setting __DYNASTY             : CIM_Setting __RELPATH             : Win32_Desktop.Name="NT AUTHORITY\\SYSTEM" __PROPERTY_COUNT      : 21 __DERIVATION          : {CIM_Setting} __SERVER              : MRED1 __NAMESPACE           : root\cimv2 __PATH                : \\MRED1\root\cimv2:Win32_Desktop.Name="NT AUTHORITY\\SY                         STEM" BorderWidth           : 1 Caption               : CoolSwitch            : True

  3. Because we are not interested in all the system properties, add the property argument to the Format-List cmdlet and choose the name property. This modified line of code is shown here:

     $objWMIService | Format-List -property name

  4. Save and run your script. Your output will only contain the name of each profile stored on your machine. It will be similar to the one shown here:

     name : NT AUTHORITY\SYSTEM name : NT AUTHORITY\LOCAL SERVICE name : NT AUTHORITY\NETWORK SERVICE name : NWTRADERS\mred name : .DEFAULT

  5. To retrieve the name of the screensaver, add the property screensaverexecutable to the Format-List command. This is shown here:

     $objWMIService | Format-List -property name, screensaverexecutable

  6. Save and run your script.

  7. To identify whether the screensaver is secure, you need to query the screensaversecure property. This modified line of code is shown here:

     $objWMIService | Format-List -property name, screensaverexecutable,    screensaverSecure

  8. If you want to retrieve all the properties related to screensavers, you can use a wild card asterisk (*) screen. Delete the two screensaver properties and replace them with the wild card, as shown here:

     $objWMIService | Format-List -property name, screen*

  9. Save and run your script. If it does not run properly, compare it to the image from book ModifiedDesktop.ps1 script.




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