|
Guy recommends: This CG will put you in charge of controlling changes to network routers and other SNMP devices. Download your free Config Generator |
Windows PowerShell - Conditional OperatorsIntroduction to PowerShell Conditional OperatorsI think of PowerShell's conditional operators as filters. Perhaps you are suffering from a common problem - too much information? If so, then choosing the most suitable PowerShell operator: -match, -like or -contains will help you to distil the key information. Introduction to: -match -like and -contains.-Match -like and -contains are all similar PowerShell conditional operators, yet each has a subtle specialization. My advice is keep experimenting until you find the particular conditional operator that suits your circumstance. Have faith that you will be able to manipulate one of this trio, and thus achieve the degree of pattern matching that you seek. Topics for PowerShell's Conditional OperatorsAlso note, these operators are in addition to the ubiquitous comparison operators, -eq, 'If' and 'elseif'. ♣ Example 1 -Match Conditional OperatorThe 'match' can be anywhere in the string. Moreover, the pattern does not have to be a complete, and this is the biggest benefit of match. -Match uses regular expressions for pattern matching. Incidentally -match, and the other PowerShell conditional operators, all have a negative form, for example -notmatch. Example 1a - Match does not have to be at the beginning
$Guy ="Guy Thomas 1949" # Result PS> True Example 1b - Naturally a completely wrong name is no good
$Guy ="Guy Thomas 1949" # Result PS> False Example 1c - Wrong date
$Guy ="Guy Thomas 1949" # Result PS> False Example 1d - Wildcard? Rides to the rescue
$Guy ="Guy Thomas 1949" # Result PS> True Example 1e - WmiObject and Where, using * as a Wildcard Here is a real-life -match example using WmiObject and a where clause, observer the Wildcard*. Get-WmiObject -list | where {$_.name -match "cim*"} Additional PowerShell Character Classes A character class is a broader form of wildcard, which represents an entire group of characters. You can use these for really wide-ranging searches. PowerShell recognizes the following character classes: \w matches any word character, meaning letters and numbers. There are also the reverse or negative versions which employ a capital letter. (Can be hard to get a false with these!) \W (capital W) any non-word character Negative -notmatch For negative conditions, there is an alternative form called -notmatch. PowerShell also has ErrorAction silentlyContinue.
˚
Example 2 -Like Conditional OperatorWith -like, both sides of the expression have to be the same, fortunately, you can employ the usual wildcards * and ? Example 2a - Having only part of the string is no good for -like
$Guy ="Guy Thomas 1949" # Result PS> False Example 2b - Just the start of the string not enough
$Guy ="Guy Thomas 1949" # Result PS> False Example 2c - Wildcard * is useful for -like
$Guy ="Guy Thomas 1949" Result PS> True Example 2d - Wildcard* but the rest has to be correct
$Guy ="Guy Thomas 1949" # Result PS> False Example 2e - * Wildcards * Double wildcards are handy
$Guy ="Guy Thomas 1949" # Result PS> True If your logic needs the negative, then try -notlike. In addition,-like -and -notlike have variants that force case sensitivity. -clike and -cnotlike. Difference Between -like and -match
$Guy ="Guy Thomas 1949" # Result PS> False
$Guy ="Guy Thomas 1949" # Result PS> True
Guy Recommends: SolarWinds LANSurveyor
|
|||||
Download my ebook:
|
*
|
|
Guy
Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2010 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |