Yep, more from MOSS!

I’ve been exploring ways I can use PowerShell to automate some tasks related to performance monitoring for MOSS and I wanted to turn the thinking into a quick post about using REGEX with PowerShell.

By no means am I a REGEX expert, in fact I’m whatever the opposite of a REGEX expert is, I’m terrified of it.  That said, using it in basic ways is not the most complex thing you’ll do this year.

There are two REGEX constructs worth understanding here:

  • \.
    • . (the full stop) is the REGEX single character wildcard, so to use a literal “.” (as in .NET) you need to escape it using the backslash resulting in \.
  • .{1,}
    • This construct is to “repeat the last character no fewer than one time and up to any number of times” by prefixing it with “.”this is a method to apply a true wildcard to my REGEX, a little like using text* to mean “the word text followed by anything else”
    • If a number is added after the comma (such as {1,4}) this would signify “no fewer than 1 and no greater than 4”

Beyond that, it’s basic PowerShell using a Where clause matching against the REGEX pattern.

 [REGEX]$counternames = "^(Process|Processor|Thread|Memory|\.NET.CLR.{1,}|ASP\.NET.v2.{1,}|ASP\.NET.Apps.v2.{1,})$" $counters = Get-Counter -ListSet "*" | ?{$_.countersetname -match $counternames} ForEach ($counter in $counters) { $counter.CounterSetName $counter.Paths } 

more to follow…