Search code examples
sql-serverpowershelldbatools

Filter datasets using Where-Object cmdlet


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


Solution

  • 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