-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetOperations.ps1
164 lines (129 loc) · 5.35 KB
/
setOperations.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<#
.SYNOPSIS
Performs "set" operations
.DESCRIPTION
Given two sets, does Union, Intersection, Difference and Complement
.INPUTS
Two sets
.OUTPUTS
The results of the set operation
.EXAMPLE
$a = (1,2,3,4)
$b = (1,3,4,5)
Get-SetOperationResult -Left $a -Right $b -OperationType Union
1
3
4
5
2
Get-SetOperationResult -Left $a -Right $b -OperationType Intersection
1
3
4
Get-SetOperationResult -Left $a -Right $b -OperationType Difference-LeftMinusRight
2
Get-SetOperationResult -Left $a -Right $b -OperationType Difference-RightMinusLeft
5
Get-SetOperationResult -Left $a -Right $b -OperationType ComplementLeft
5
Get-SetOperationResult -Left $a -Right $b -OperationType ComplementRight
2
.EXAMPLE
#Find the properties in first object that are not in second!
# Notice that the Create/Update columns do not exist in the second object
$databaseTypeObject1 = New-Object System.Object
$databaseTypeObject1 | Add-Member -type NoteProperty -name DatabaseType -value 'Oracle'
$databaseTypeObject1 | Add-Member -type NoteProperty -name Vendor -value 'Oracle Corporation'
$databaseTypeObject1 | Add-Member -type NoteProperty -name DatabaseName -value 'Oracle'
$databaseTypeObject1 | Add-Member -type NoteProperty -name Description -value 'Oracle database'
$databaseTypeObject1 | Add-Member -type NoteProperty -name CreateUser -value $env:USERNAME
$databaseTypeObject1 | Add-Member -type NoteProperty -name CreateDate -value (Get-Date)
$databaseTypeObject1 | Add-Member -type NoteProperty -name UpdateUser -value $env:USERNAME
$databaseTypeObject1 | Add-Member -type NoteProperty -name UpdateDate -value (Get-Date)
$databaseTypeObject2 = New-Object System.Object
$databaseTypeObject2 | Add-Member -type NoteProperty -name DatabaseType -value 'Sybase'
$databaseTypeObject2 | Add-Member -type NoteProperty -name Vendor -value 'SAP1'
$databaseTypeObject2 | Add-Member -type NoteProperty -name DatabaseName -value 'Sybase'
$databaseTypeObject2 | Add-Member -type NoteProperty -name Description -value 'Sybase'
Get-SetOperationResult `
-Left ($databaseTypeObject1 | Get-Member -MemberType Properties | Select-Object -Expand Name) `
-Right ($databaseTypeObject2 | Get-Member -MemberType Properties | Select-Object -Expand Name) `
-OperationType Difference-LeftMinusRight
.NOTES
Pretty self-explanatory. See "Set Theory" for a refresher link below if needed.
Version History
Created by Jana Sattainathan | Twitter @SQLJana | WordPress: SQLJana.WordPress.com
- Used idea from referenced URL and built this function
.LINK
http://stackoverflow.com/questions/8609204/union-and-intersection-in-powershell
http://www.cs.odu.edu/~toida/nerzic/content/set/set_operations.html
#>
function Get-SetOperationResult
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,
Position=0)]
[object[]]
$Left,
[Parameter(Mandatory=$true,
Position=1)]
[object[]]
$Right,
[Parameter(Mandatory=$false,
Position=2)]
[ValidateSet("Union","Intersection","Difference-LeftMinusRight","Difference-RightMinusLeft","ComplementLeft","ComplementRight")]
[string]
$OperationType="Intersection"
)
BEGIN
{
}
PROCESS
{
[object] $result = @()
#-----------
#Union = Given two sets, the distinct set of values from both
#-----------
if ($OperationType -eq 'Union')
{
$result = Compare-Object $Left $Right -PassThru -IncludeEqual # union
}
#-----------
#Intersection = Given two sets, the distinct set of values that are only in both
#-----------
if ($OperationType -eq 'Intersection')
{
$result = Compare-Object $Left $Right -PassThru -IncludeEqual -ExcludeDifferent # intersection
}
#-----------
#Difference = Given two sets, the values in one (minus) the values in the other
#-----------
if ($OperationType -eq 'Difference-LeftMinusRight')
{
$result = $Left | ?{-not ($Right -contains $_)}
}
if ($OperationType -eq 'Difference-RightMinusLeft')
{
$result = $Right | ?{-not ($Left -contains $_)}
}
#-----------
#Complement = Given two sets, everything in the universe which is the UNION (minus) the values in the set being "Complemented"
#-----------
if ($OperationType -eq 'ComplementLeft')
{
$result = Compare-Object $Left $Right -PassThru -IncludeEqual | # union
?{-not ($Left -contains $_)}
}
if ($OperationType -eq 'ComplementRight')
{
$result = Compare-Object $Left $Right -PassThru -IncludeEqual | # union
?{-not ($Right -contains $_)}
}
Write-Output $result
}
END
{
}
}