比较Powershell中包含对象的两个列表



我在Powershell中创建了一个对象,如下所示:

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())
}    
}

我有两个ArrayList,其中包含来自不同来源的groupObject

现在我想比较一下这两个列表,找出只在其中一个列表中的所有组。我正在尝试使用类似于$onlyList2= $List2 | ?{$List1 -notcontains $_}的东西。但我不确定如何使用我的groupObjects来做到这一点。有什么建议吗?

没错,Compare Object就是答案:使用PowerShell代码,您可以添加:

$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

这将是结果:

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 =>

相关内容

  • 没有找到相关文章

最新更新