-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCompare-ObjectProperty.ps1
74 lines (63 loc) · 2.61 KB
/
Compare-ObjectProperty.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
function Compare-ObjectProperty {
<#
.SYNOPSIS
Compares two objects property by property.
.DESCRIPTION
Compares two objects property by property. A simple Compare-Object only compares those properties with the same name in the two objects.
.PARAMETER ReferenceObject
The first object to compare
.PARAMETER DifferenceObject
The second object to compare
.EXAMPLE
$a = New-Object psobject -Prop ([ordered] @{ One = 1; Two = 2})
$b = New-Object psobject -Prop ([ordered] @{ One = 1; Two = 2; Three = 3})
Compare-Object $a $b
# would return $null because it only compares the properties that have common names but
Compare-ObjectProperty $a $b
# would return below because it compares the two objects property by property
PropertyName RefValue DiffValue
------------ -------- ---------
Three 3
.OUTPUTS
[psobject]
.LINK
Compare-Object
#>
#region Parameters
[CmdletBinding(ConfirmImpact = 'None')]
[OutputType('psobject')]
Param(
[Parameter(Mandatory, HelpMessage = 'First object to compare', Position = 0)]
[PSObject] $ReferenceObject,
[Parameter(Mandatory, HelpMessage = 'Second object to compare', Position = 1)]
[PSObject] $DifferenceObject
)
#endregion Parameters
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
$objprops = New-Object -TypeName System.Collections.ArrayList
}
process {
$null = $objprops.AddRange(($ReferenceObject | Get-Member -MemberType Property, NoteProperty).Name)
$null = $objprops.AddRange(($DifferenceObject | Get-Member -MemberType Property, NoteProperty).Name)
$objprops = $objprops | Sort-Object | Select-Object -Unique
$diffs = New-Object -TypeName System.Collections.ArrayList
foreach ($objprop in $objprops) {
$diff = Compare-Object -ReferenceObject $ReferenceObject -DifferenceObject $DifferenceObject -Property $objprop
if ($diff) {
$diffprops = @{
PropertyName = $objprop
RefValue = ($diff | Where-Object { $_.SideIndicator -eq '<=' } | ForEach-Object $($objprop))
DiffValue = ($diff | Where-Object { $_.SideIndicator -eq '=>' } | ForEach-Object $($objprop))
}
$null = $diffs.Add((New-Object -TypeName PSObject -Property $diffprops))
}
}
if ($diffs) {
$diffs | Select-Object -Property PropertyName, RefValue, DiffValue
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}