forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCompare-Properties.ps1
111 lines (100 loc) · 2.96 KB
/
Compare-Properties.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
<#
.SYNOPSIS
Compares the properties of two objects.
.INPUTS
System.Management.Automation.PSObject with properties to compare.
.OUTPUTS
System.Management.Automation.PSCustomObject for each relevant property comparison,
with these fields:
* PropertyName
* Reference
* Value
* Difference
* DifferentValue
.FUNCTIONALITY
Properties
.LINK
https://docs.microsoft.com/dotnet/api/system.management.automation.psmemberset
.EXAMPLE
Compare-Properties.ps1 (Get-PSProvider variable) (Get-PSProvider alias)
PropertyName : ImplementingType
Reference : True
Value : Microsoft.PowerShell.Commands.VariableProvider
Difference : True
DifferentValue : Microsoft.PowerShell.Commands.AliasProvider
PropertyName : Name
Reference : True
Value : Variable
Difference : True
DifferentValue : Alias
PropertyName : Drives
Reference : True
Value : {Variable}
Difference : True
DifferentValue : {Alias}
#>
#Requires -Version 3
[CmdletBinding()][OutputType([Management.Automation.PSCustomObject])] Param(
# The base object to compare properties to.
[Parameter(Position=0)][PSObject] $ReferenceObject,
# The second object to compare the properties of.
[Parameter(Position=1,ValueFromPipeline=$true)][PSObject] $DifferenceObject,
# Indicates different values should be suppressed.
[switch] $ExcludeDifferent,
# Indicates equal values should be included.
[switch] $IncludeEqual
)
Begin
{
[string[]] $referenceProperties = @()
$referenceProperties += $ReferenceObject.PSObject.Properties.Match('*','Properties') |Select-Object -ExpandProperty Name
}
Process
{
[string[]] $differenceProperties = @()
$differenceProperties += $DifferenceObject.PSObject.Properties.Match('*','Properties') |Select-Object -ExpandProperty Name
foreach($property in $referenceProperties)
{
$referenceValue = $ReferenceObject.$property
if(!$DifferenceObject.PSObject.Properties.Match($property,'Properties').Count)
{
if(!$ExcludeDifferent)
{
[pscustomobject]@{
PropertyName = $property
Reference = $true
Value = $referenceValue
Difference = $false
DifferentValue = $null
}
}
}
else
{
$differentValue = $DifferenceObject.$property
$comparison = [pscustomobject]@{
PropertyName = $property
Reference = $true
Value = $referenceValue
Difference = $true
DifferentValue = $differentValue
}
if($referenceValue -eq $differentValue) {if($IncludeEqual) {$comparison}}
elseif(!$ExcludeDifferent) {$comparison}
}
}
if(!$ExcludeDifferent)
{
foreach($property in $differenceProperties |
Where-Object {!$ReferenceObject.PSObject.Properties.Match($_,'Properties').Count})
{
[pscustomobject]@{
PropertyName = $property
Reference = $false
Value = $null
Difference = $true
DifferentValue = $DifferenceObject.$property
}
}
}
}