-
Notifications
You must be signed in to change notification settings - Fork 28
/
Get-ObjectsInTwoArrays.ps1
45 lines (41 loc) · 1.23 KB
/
Get-ObjectsInTwoArrays.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function Get-ObjectsInTwoArrays {
[CmdletBinding()]
param (
[Parameter()]
$Array,
[Parameter()]
$ArrayToCompare,
[Parameter(Mandatory)]
[ValidateSet('In', 'NotIn')]
$ComparisonMethod,
[Parameter(Mandatory)]
[ValidateSet('String', 'int','PSObject')]
$ObjectType
)
if($ObjectType -eq 'String'){
$arrayIndex = [System.Collections.Generic.HashSet[String]]$Array
}
elseif($ObjectType -eq 'int'){
$arrayIndex = [System.Collections.Generic.HashSet[int]]$Array
}
elseif($ObjectType -eq 'PSObject'){
$arrayIndex = [System.Collections.Generic.HashSet[PSObject]]$Array
}
[System.Collections.Generic.List[PSObject]]$res = @()
if ($ComparisonMethod -eq 'In') {
foreach ($object in $ArrayToCompare) {
if ($arrayIndex.Contains($object)) {
$res.Add($object)
}
}
}
elseif ($ComparisonMethod -eq 'NotIn') {
foreach ($object in $ArrayToCompare) {
if (-not($arrayIndex.Contains($object))) {
$res.Add($object)
}
}
}
Write-Host "$($res.count) $ComparisonMethod two arrays"
return $res
}