0

I'm looking to filter datasets with a Where-Object cmdlet. For instance, consider the Notifications column in the following cmdlet. It contains two values and I would like to filter it by Where-Object | {$_.Notifications -eq 'Operator1'}. I also tried to filter using -in, -notin, -contains, -notcontains, -match, -notmatch, -like, -notlike etc. But none of these have yielded any results so far. Any pointers is highly appreciated.

PS>Get-DbaAgentAlert -SqlInstance 'Redacted'

ComputerName          : Redacted
SqlInstance           : Redacted
************          : ************
************          : ************
Notifications         : {Operator1, Operator2}
************          : ************
************          : ************

Doing a Get-Member returns

PS>Get-DbaAgentAlert -SqlInstance 'Redacted' | Get-Member

   TypeName: System.Management.Automation.PSCustomObject

Name                  MemberType   Definition
----                  ----------   ----------
OtherColumns          **********  ***********
Notifications         NoteProperty DataTable Notifications=

Also, The actual dataset for Notifications column would look like

PS>$alerts.Notifications | Select -First 2 | Format-Table

OperatorId OperatorName     UseEmail UsePager UseNetSend HasEmail HasPager HasNetSend
---------- ------------     -------- -------- ---------- -------- -------- ----------
         1 Operator1          True    False      False     True    False      False
         2 Operator2          True    False      False     True    False      False

Thanks!

Edit: Source of the cmdlet I'm using here is from dbatools/Get-DbaAgentAlert

alroc
  • 27,574
  • 6
  • 51
  • 97
walt
  • 3
  • 2
  • Have you tried `-contains`? The Notifications looks like an array. – gvee Mar 15 '17 at 21:14
  • Yes, I did (mentioned in the question). But, it did not return any results. I should have put the source of the command that I was using here. It's from this module - [dbatools](https://dbatools.io/) – walt Mar 15 '17 at 21:20
  • I'm not really clear on what part of the result you want to filter. The cmdlet returns some Alerts, each of which contains some Notifications, each row of which contains multiple values. If you try to compare a string against a DataRow (a single entry from the Notifications DataTable), of course that will fail. You need to take into account the DataTable columms (like "OperatorName" in your example) – TToni Mar 15 '17 at 21:34

1 Answers1

0

Try this:

Get-DbaAgentAlert -SqlInstance ".\sql2016" |
Where-Object {$_.Notifications.OperatorName -eq "test1"}

Here's how I worked that out:

$results = Get-DbaAgentAlert -SqlInstance ".\sql2016"
$res.Notifications

This returns something like:

OperatorId   : 1
OperatorName : test1
UseEmail     : True
UsePager     : False
UseNetSend   : False
HasEmail     : False
HasPager     : False
HasNetSend   : False

OperatorId   : 2
OperatorName : test2
UseEmail     : True
UsePager     : False
UseNetSend   : False
HasEmail     : False
HasPager     : False
HasNetSend   : False

...the Notifications property is basically another object, so we have to target the correct element of that object

gvee
  • 16,732
  • 35
  • 50