MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
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.
To query WMI:
-
Specify the computer name.
-
Define the namespace.
-
Connect to WMI by using Get-WMIObject.
-
Specify the query by supplying a value for the filter argument or the query argument.
-
Use Format-List or another cmdlet to clean up the output.
In the
$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
$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
-
Open the
QueryDesktop.ps1 script in Notepad.exe or your favorite script editor. Save the script as yourname ModifiedDesktop.ps1. -
Run the yourname
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
-
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
-
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
-
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
-
Save and run your script.
-
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
-
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*
-
Save and run your script. If it does not run properly, compare it to the
ModifiedDesktop.ps1 script.
Категории