1

I created a object in Powershell which looks like this:

Class groupObject{ 
    [string] $Name
    [string] $Domain

    groupObject([string] $inputName, [string] $inputDomain){
        $this.Name = $inputName
        $this.Domain = $inputDomain
    }

    setName([string] $inputName){ 
        $this.Name = $inputName 
    }

    setDomain([string] $inputDomain){ 
        $this.Domain = $inputDomain 
    } 

    [string] getName(){ 
        return $this.Name
    }

    [string] getDomain(){ 
        return $this.Domain
    }

    # Compare two groupObjects.
    [boolean] isEqual([groupObject] $ADgroup){
        return ($ADgroup.getName() -eq $this.getName() -and $ADgroup.getDomai() -eq $this.getDomain())
    }    
}

And I've got two ArrayLists containing groupObjects from different sources.

Now I want to compare those two lists and find all groups which are only in one of them. I am trying to use something like this $onlyList2= $List2 | ?{$List1 -notcontains $_}. But I'm not sure how I can do this using my groupObjects. Any suggestions?

1
  • 1
    Have you tried the Compare-Object cmdlet? Commented Nov 9, 2018 at 9:47

1 Answer 1

2

It's true, Compare-Object is the answer: with your PowerShell code, you can add:

$ListLeft = @(
    [groupObject]::new('NameLeft1', 'DomainLeft')
    [groupObject]::new('NameBoth1', 'DomainBoth')
)

$ListRight = @(
    [groupObject]::new('NameRight1', 'DomainRight')
    [groupObject]::new('NameBoth1', 'DomainBoth')
)


'Records which are unique in $ListLeft, comparing Name and Domain:'
Compare-Object -ReferenceObject $ListLeft -DifferenceObject $ListRight -Property 'Name','Domain' | Where-Object SideIndicator -EQ '<=' | FT

'Records which are unique in $ListRight, comparing Name and Domain:'
Compare-Object -ReferenceObject $ListLeft -DifferenceObject $ListRight -Property 'Name', 'Domain' | Where-Object SideIndicator -EQ '=>' | FT

This will be the result:

Records which are unique in $ListLeft, comparing Name and Domain:

Name      Domain     SideIndicator
----      ------     -------------
NameLeft1 DomainLeft <=

Records which are unique in $ListRight, comparing Name and Domain:

Name       Domain      SideIndicator
----       ------      -------------
NameRight1 DomainRight =>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.