-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCompare-Keys.ps1
116 lines (106 loc) · 3.06 KB
/
Compare-Keys.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<#
.SYNOPSIS
Returns the differences between two dictionaries.
.INPUTS
System.Collections.IDictionary to compare to the reference dictionary.
.OUTPUTS
System.Management.Automation.PSObject with these properties:
* Key: The dictionary key being compared.
* Action: A Data.DataRowState that indicates whether the key-value pair has been
Added, Deleted, Modified, or Unchanged.
* ReferenceValue: The original value.
* DifferenceValue: The new value.
.FUNCTIONALITY
Dictionary
.LINK
Compare-Object
.LINK
Group-Object
.LINK
ForEach-Object
.LINK
Sort-Object
.EXAMPLE
Compare-Keys.ps1 @{ A = 1; B = 2; C = 3 } @{ D = 6; C = 4; B = 2 } -IncludeEqual
Key Action ReferenceValue DifferenceValue
--- ------ -------------- ---------------
A Deleted 1
B Unchanged 2 2
C Modified 3 4
D Added 6
#>
#Requires -Version 7
[CmdletBinding()] Param(
# The original dictionary to compare.
[Parameter(Position=0,Mandatory=$true)][Collections.IDictionary] $ReferenceDictionary,
# A dictionary to compare to the original.
[Parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true)][Collections.IDictionary] $DifferenceDictionary,
# Indicates that different values should be ignored.
[switch] $ExcludeDifferent,
# Indicates that identical values should be included.
[switch] $IncludeEqual
)
Process
{
if([Environment]::Version.Major -lt 7)
{
$refEntries = @($ReferenceDictionary.GetEnumerator()) |
Add-Member -MemberType ScriptMethod -Name ToString -Value {'[{0}, {1}]' -f $this.Key, $this.Value} -Force -PassThru
$diffEntries = @($DifferenceDictionary.GetEnumerator()) |
Add-Member -MemberType ScriptMethod -Name ToString -Value {'[{0}, {1}]' -f $this.Key, $this.Value} -Force -PassThru
}
else
{
$refEntries = @($ReferenceDictionary.GetEnumerator())
$diffEntries = @($DifferenceDictionary.GetEnumerator())
}
Compare-Object $refEntries $diffEntries -ExcludeDifferent:$ExcludeDifferent -IncludeEqual:$IncludeEqual |
Group-Object {$_.InputObject.Key} |
ForEach-Object {
$key = $_.Values[0]
if($_.Group.Count -gt 1)
{
$refValue, $diffValue = ($_.Group |Sort-Object SideIndicator).InputObject.Value
[pscustomobject]@{
Key = $key
Action = [Data.DataRowState]::Modified
ReferenceValue = $refValue
DifferenceValue = $diffValue
}
}
else
{
$value = $_.Group[0].InputObject.Value
switch($_.Group[0].SideIndicator)
{
'<='
{
[pscustomobject]@{
Key = $key
Action = [Data.DataRowState]::Deleted
ReferenceValue = $value
DifferenceValue = $null
}
}
'=='
{
[pscustomobject]@{
Key = $key
Action = [Data.DataRowState]::Unchanged
ReferenceValue = $value
DifferenceValue = $value
}
}
'=>'
{
[pscustomobject]@{
Key = $key
Action = [Data.DataRowState]::Added
ReferenceValue = $null
DifferenceValue = $value
}
}
}
}
}
}