Compare arrays to get those that don’t match

Welcome Forums General PowerShell Q&A Compare arrays to get those that don’t match

Viewing 2 reply threads
  • Author
    Posts
    • #226467
      Participant
      Topics: 48
      Replies: 194
      Points: 767
      Helping Hand
      Rank: Major Contributor

      I’ve two arrays: $Servers and $obj, both contain Node and Instance.

      How can I get the values in $obj that doesn’t exist in $Servers?

      I’ve tried Compare-Object and also tried

       $Servers | Where { $Obj -NotContains $_}

      which gives me the exact opposite of what I want, but when I try

       $obj | Where { $Servers -NotContains $_}

      it includes values it should not.

      eg: $Obj

      
      Node Instance
      ---- --------
      SQNDE-P03 COMMAND
      SQNDE-P03 PROD03
      SQNDE-P03 PROD04
      SQNDE-P03 PROD05
      SQNDE-P02 COMMAND
      SQNDE-P02 PROD03
      SQNDE-P02 PROD04
      SQNDE-P02 PROD05
      SQNDE-P01 COMMAND
      SQNDE-P01 PROD03
      SQNDE-P01 PROD04
      SQNDE-P01 PROD05
      
      

       

      eg: $Servers

      
      Node Instance
      ---- --------
      SQNDE-P03 PROD03
      SQNDE-P02 PROD05
      SQNDE-P01 DPROD04
      SQNDE-P01 COMMAND
      
      
    • #226470
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      I’ve tried Compare-Object and also tried

      Did you specify the properties with Compare-Object?

      $obj = @"
      Node,Instance
      SQNDE-P03,COMMAND
      SQNDE-P03,PROD03
      SQNDE-P03,PROD04
      SQNDE-P03,PROD05
      SQNDE-P02,COMMAND
      SQNDE-P02,PROD03
      SQNDE-P02,PROD04
      SQNDE-P02,PROD05
      SQNDE-P01,COMMAND
      SQNDE-P01,PROD03
      SQNDE-P01,PROD04
      SQNDE-P01,PROD05
      "@ | ConvertFrom-CSV
      
      
      $servers = @"
      Node,Instance
      SQNDE-P03,PROD03
      SQNDE-P02,PROD05
      SQNDE-P01,DPROD04
      SQNDE-P01,COMMAND
      "@ | ConvertFrom-CSV
      
      Compare-Object -ReferenceObject $obj -DifferenceObject $servers -Property Node,Instance
      

      Output:

      Node      Instance SideIndicator
      ----      -------- -------------
      SQNDE-P01 DPROD04  =>
      SQNDE-P03 COMMAND  <=
      SQNDE-P03 PROD04   <=
      SQNDE-P03 PROD05   <=
      SQNDE-P02 COMMAND  <=
      SQNDE-P02 PROD03   <=
      SQNDE-P02 PROD04   <=
      SQNDE-P01 PROD03   <=
      SQNDE-P01 PROD04   <=
      SQNDE-P01 PROD05   <=
      
    • #226482
      Participant
      Topics: 48
      Replies: 194
      Points: 767
      Helping Hand
      Rank: Major Contributor

      I hadn’t specified properties, doing so gives the result expected.

      Thanks for your help!

       

Viewing 2 reply threads
  • You must be logged in to reply to this topic.