|
Guy recommends: This CG will put you in charge of controlling changes to network routers and other SNMP devices. Download your free Config Generator |
PowerShell -Filter ParameterPowerShell -Filter ParameterPowerShell's -filter is a neater solution for sieving data than the | where-Object {$_.clause}. Using -filter instead of 'where-Object' is a classic case of when you find a good technique, look for an even better modus operandi. Topics for PowerShell Filters♣ Preparation with PowerShell's Get-HelpBefore we use PowerShell's -filter parameter, I suggest that we investigate the cmdlet with get-Help. For example, get-Help get-WmiObject. My point is that we need to check that any particular cmdlet supports the -filter parameter. # Help with PowerShell WMI object: Note 1: If you prefer to see examples, this is why I append -full. Note 2: Try other cmdlets such as get-ChildItem (contains -filter) or format-Table (no -filter parameter). Example 1: PowerShell Filter FilesHere is a simple example to reduce the number of files displayed only those which are dlls. # PowerShell Filter for dll files Example 2: PowerShell get-WmiObject FilterPowerShell's -Filter uses the syntax of the WMI Query Language (WQL), which is a subset of SQL. One point to clarify is that WQL uses 'traditional' operators such as "=", whereas PowerShell uses "-eq". a) Preliminary: No Filter # PowerShell get-WmiObject Note 1: The problem could be too many items, or a mixed bag results. Suppose that we wanted to perform an operation on only some of the returned items? The answer is to employ PowerShell and filter the output. b) Same Command, but Filtered for IpEnabled Adapters # PowerShell get-WmiObject Filter Note 1: Remember the "Double Quotes" around your -Filter parameter. Note 2: = 'True', or ='False' is correct. WQL does not understand PowerShell's -eq or -neq. Note 3: -Filter is technically superior to methods such as 'Where' or 'Include' because it sifts the objects as it retrieves them. c) PowerShell Where Filter for Comparison # PowerShell get-WmiObject -Filter replaced with where-Object Note 1: Where-Object achieves exactly the same result, but it needs an extra pipe (|). My problem is remembering the $_. syntax. I often forget the _ as in: $.IpEnabled. PowerShell -filter output is shorter than the where-Object clause.
Guy
Recommends: WMI Monitor and Its Free!
|
|||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2010 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |