-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compare-ArrayOrder.ps1
67 lines (57 loc) · 1.8 KB
/
Compare-ArrayOrder.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function Compare-ArrayOrder {
<#
.SYNOPSIS
Compare small arrays against each other, including order of elements.
.NOTES
For larger arrays, use this logic.
$diff = [Collections.Generic.HashSet[string]]$Collection1
$diff.SymmetricExceptWith([Collections.Generic.HashSet[string]]$Collection2.BaseName)
$diffArray = [string[]]$diff
#>
[CmdletBinding()]
param (
[Parameter(Mandatory,Position=0)]
[ValidateNotNullOrEmpty()]
[system.object[]]
$ReferenceArray,
[Parameter(Mandatory,Position=1)]
[ValidateNotNullOrEmpty()]
[system.object[]]
$DifferenceArray,
[Parameter()]
[switch]
$Quiet
)
$doesItMatch = $true
$failedItems = [system.collections.arraylist]@()
if (
$ReferenceArray.Count -ne
$DifferenceArray.Count
) {
Write-Warning "Arrays have different counts"
$doesItMatch = $false
[void]($failedItems.Add([pscustomobject]@{
index = -1
ReferenceValue = $ReferenceArray.Count
DifferenceValue = [math]::Abs(($ReferenceArray.Count - $DifferenceArray.Count))
}))
}
for ($i = 0; $i -lt $ReferenceArray.Count; $i++) {
if ($ReferenceArray[$i] -ne $DifferenceArray[$i]) {
Write-Warning "item[$i] doesn't match: $(
$ReferenceArray[$i]):$($DifferenceArray[$i]
)"
$doesItMatch = $false
[void]($failedItems.Add([pscustomobject]@{
index = $i
ReferenceValue = $ReferenceArray[$i]
DifferenceValue = $DifferenceArray[$i]
}))
}
}
if ($Quiet) {
$doesItMatch
} else {
$failedItems
}
}#END: function Compare-ArrayOrder