-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJoin-Object.Examples.ps1
152 lines (144 loc) · 4.58 KB
/
Join-Object.Examples.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
$ExampleData = {
# Left Object Example
$PSCustomObject = @(
[PSCustomObject]@{ ID = 1 ; Sub = 'S1' ; IntO = 6 }
[PSCustomObject]@{ ID = 2 ; Sub = 'S2' ; IntO = 7 }
[PSCustomObject]@{ ID = 3 ; Sub = 'S3' ; IntO = $null }
)
# Right Object Example (DataTable)
$DataTable = [Data.DataTable]::new('Test')
$null = $DataTable.Columns.Add('IDD', [System.Int32])
$null = $DataTable.Columns.Add('Name')
$null = $DataTable.Columns.Add('Junk')
$null = $DataTable.Columns.Add('IntT', [System.Int32])
$null = $DataTable.Rows.Add(1, 'foo', 'AAA', 123456)
$null = $DataTable.Rows.Add(3, 'Bar', 'S3', $null)
$null = $DataTable.Rows.Add(4, 'D', $null, $null)
}
. $ExampleData
# Example 1: Join the 2 together ("Left Join" in this case).
Join-Object -Left $PSCustomObject -Right $DataTable -LeftJoinProperty 'ID' -RightJoinProperty 'IDD' | Format-Table
<# Output
ID Sub IntO Name Junk IntT
-- --- ---- ---- ---- ----
1 S1 6 foo AAA 123456
2 S2 7
3 S3 Bar S3
#>
. $ExampleData
# Example 2A: Filtering columns.
$Params = @{
Left = $PSCustomObject
Right = $DataTable
LeftJoinProperty = 'ID'
RightJoinProperty = 'IDD'
ExcludeRightProperties = 'Junk' # Exclude column "Junk" from the right columns.
Prefix = 'R_' # Add Prefix to the right columns.
LeftProperties = 'ID', 'Sub' # Select columns to include from the right.
}
Join-Object @Params | Format-Table
<# Output
ID Sub R_Name R_IntT
-- --- ------ ------
1 S1 foo 123456
2 S2
3 S3 Bar
#>
# Example 2B: Filtering renaming and reordering columns.
$Params['LeftProperties'] = [ordered]@{ Sub = 'Subscription' ; ID = 'ID' } # Select columns to include from the right, rename and reorder them.
Join-Object @Params | Format-Table
<# Output
Subscription ID R_Name R_IntT
------------ -- ------ ------
S1 1 foo 123456
S2 2
S3 3 Bar
#>
. $ExampleData
# Example 3: -Type. Options: AllInLeft (default), OnlyIfInBoth, AllInBoth.
$Params = @{
Left = $PSCustomObject
Right = $DataTable
LeftJoinProperty = 'ID'
RightJoinProperty = 'IDD'
Type = 'OnlyIfInBoth'
}
Join-Object @Params | Format-Table
<# Output
ID Sub IntO Name Junk IntT
-- --- ---- ---- ---- ----
1 S1 6 foo AAA 123456
3 S3 Bar S3
#>
. $ExampleData
# Example 4: Output format. (When input is [DataTable] containing [DBNull]s if output is [PSCustomObject] they will be converted to $null).
$Params = @{
Left = $PSCustomObject
Right = $DataTable
LeftJoinProperty = 'ID'
RightJoinProperty = 'IDD'
DataTable = $true # By default output format is PSCustomObject this changes it to DataTable.
}
Join-Object @Params | Format-Table
<# This is a DataTable
ID Sub IntO Name Junk IntT
-- --- ---- ---- ---- ----
1 S1 6 foo AAA 123456
2 S2 7
3 S3 Bar S3
#>
. $ExampleData
# Example 5: -PassThru. Editing the existing left object preserving it's existing type PSCustomObject/DataTable.
$Params = @{
Left = $PSCustomObject
Right = $DataTable
LeftJoinProperty = 'ID'
RightJoinProperty = 'IDD'
PassThru = $true
}
$null = Join-Object @Params
$PSCustomObject | Format-Table
<# $PSCustomObject changed to:
ID Sub IntO Name Junk IntT
-- --- ---- ---- ---- ----
1 S1 6 foo AAA 123456
2 S2 7
3 S3 Bar S3
#>
. $ExampleData
# Example 6: JoinScript. Manipulate the JoinProperty for the comparison with a Scriptblock.
$Params = @{
Left = $PSCustomObject
Right = $DataTable
LeftJoinProperty = 'Sub'
RightJoinProperty = 'IDD'
LeftJoinScript = { param ($Line) $Line.Sub.Replace('S', '')} # For example change "Sub" column value from "S1" to "1" to compare to "IDD" column "1".
}
Join-Object @Params | Format-Table
<# Output
ID Sub IntO Name Junk IntT
-- --- ---- ---- ---- ----
1 S1 6 foo AAA 123456
2 S2 7
3 S3 Bar S3
#>
. $ExampleData
# Example 7: -AddKey. can be used with "-Type AllInBoth" to add a column containing the joining key.
$Params = @{
Left = $PSCustomObject
Right = $DataTable
LeftJoinProperty = 'ID'
RightJoinProperty = 'IDD'
LeftProperties = 'Sub'
Type = 'AllInBoth'
AddKey = 'Index'
}
Join-Object @Params | Format-Table
<# Output
Index Sub Name Junk IntT
----- --- ---- ---- ----
1 S1 foo AAA 123456
2 S2
3 S3 Bar S3
4 D
#>