I am working on a SCOM script to sort alerts based on MonitoringObjectName , Since there are servers with different names , I want to match this (MonitoringObjectName) property string against a list of keywords. I was wondering how can I match the property against the keywords placed in an array
$keywords = @("ABC","DEF","XYZ","GHI")
Get-SCOMAlert | ? ($_.MonitoringObjectName -like "*$keyword*"
Looking for ideas/hints how this can be done
Either use a regular expression match like this:
$keywords = 'ABC|DEF|XYZ|GHI'
Get-SCOMAlert | ? { $_.MonitoringObjectName -match $keywords }
alternatively (if you want to keep the keywords as a list):
$keywords = 'ABC', 'DEF', 'XYZ', 'GHI'
$re = $keywords -join '|'
Get-SCOMAlert | ? { $_.MonitoringObjectName -match $re }
or do a wildcard comparison in a nested Where-Object
:
$keywords = 'ABC', 'DEF', 'XYZ', 'GHI'
Get-SCOMAlert | ? {
$name = $_.MonitoringObjectName
$keywords | ? { $name -like "*$_*" }
}