Find duplicate elements in the array and compare them

Viewed 21

I am working on a script and i need help to accomplish this.

This is the array.

@{VMHost=**host1**; HBA=vmhba1; Targets=1; Devices=2; **Paths=5**}
@{VMHost=**host1**; HBA=vmhba2; Targets=2; Devices=3; **Paths=6**}
@{VMHost=host2; HBA=vmhba1; Targets=2; Devices=3; Paths=6}
@{VMHost=host2; HBA=vmhba2; Targets=2; Devices=3; Paths=6}
@{VMHost=host3; HBA=vmhba1; Targets=2; Devices=3; Paths=6}
@{VMHost=host3; HBA=vmhba2; Targets=2; Devices=3; Paths=6}

host1 which is in the first line in the array should compare itself by host1 which is in the second line in the array. If any differences such as paths count or device count, it has to write an output.

For example: It checks the elements, duplicate host lines has been found. It compares host1 paths and it found mismatched paths. It will write an output.

Thanks.

1 Answers

Use the Group-Object command to group the objects based on different property values:

$data |Group-Object VMHost |Where-Object Count -gt 1|ForEach-Object {
  [pscustomobject]@{
    VMHost = $_.Group[0].VMHost
    DuplicateCount = $_.Count
    DistinctConfigurationCount = @($_.Group |Group Devices,Paths -NoElement)
  }
}

Which, for the sample input you've posted, would give you a result like this:

VMHost DuplicateCount DistinctConfigurationCount
------ -------------- --------------------------
host1               2                          2
host2               2                          1
Related