1
+ function Add-ObjectDetail
2
+ {
3
+ <#
4
+ . SYNOPSIS
5
+ Decorate an object with
6
+ - A TypeName
7
+ - New properties
8
+ - Default parameters
9
+
10
+ . DESCRIPTION
11
+ Helper function to decorate an object with
12
+ - A TypeName
13
+ - New properties
14
+ - Default parameters
15
+
16
+ . PARAMETER InputObject
17
+ Object to decorate. Accepts pipeline input.
18
+
19
+ . PARAMETER TypeName
20
+ Typename to insert.
21
+
22
+ This will show up when you use Get-Member against the resulting object.
23
+
24
+ . PARAMETER PropertyToAdd
25
+ Add these noteproperties.
26
+
27
+ Format is a hashtable with Key (Property Name) = Value (Property Value).
28
+
29
+ Example to add a One and Date property:
30
+
31
+ -PropertyToAdd @{
32
+ One = 1
33
+ Date = (Get-Date)
34
+ }
35
+
36
+ . PARAMETER DefaultProperties
37
+ Change the default properties that show up
38
+
39
+ . PARAMETER Passthru
40
+ Whether to pass the resulting object on. Defaults to true
41
+
42
+ . EXAMPLE
43
+ #
44
+ # Create an object to work with
45
+ $Object = [PSCustomObject]@{
46
+ First = 'Cookie'
47
+ Last = 'Monster'
48
+ Account = 'CMonster'
49
+ }
50
+
51
+ #Add a type name and a random property
52
+ Add-ObjectDetail -InputObject $Object -TypeName 'ApplicationX.Account' -PropertyToAdd @{ AnotherProperty = 5 }
53
+
54
+ # First Last Account AnotherProperty
55
+ # ----- ---- ------- ---------------
56
+ # Cookie Monster CMonster 5
57
+
58
+ #Verify that get-member shows us the right type
59
+ $Object | Get-Member
60
+
61
+ # TypeName: ApplicationX.Account ...
62
+
63
+ . EXAMPLE
64
+ #
65
+ # Create an object to work with
66
+ $Object = [PSCustomObject]@{
67
+ First = 'Cookie'
68
+ Last = 'Monster'
69
+ Account = 'CMonster'
70
+ }
71
+
72
+ #Add a random property, set a default property set so we only see two props by default
73
+ Add-ObjectDetail -InputObject $Object -PropertyToAdd @{ AnotherProperty = 5 } -DefaultProperties Account, AnotherProperty
74
+
75
+ # Account AnotherProperty
76
+ # ------- ---------------
77
+ # CMonster 5
78
+
79
+ #Verify that the other properties are around
80
+ $Object | Select -Property *
81
+
82
+ # First Last Account AnotherProperty
83
+ # ----- ---- ------- ---------------
84
+ # Cookie Monster CMonster 5
85
+
86
+ . NOTES
87
+ This breaks the 'do one thing' rule from certain perspectives...
88
+ The goal is to decorate an object all in one shot
89
+
90
+ This abstraction simplifies decorating an object, with a slight trade-off in performance. For example:
91
+
92
+ 10,000 objects, add a property and typename:
93
+ Add-ObjectDetail: ~4.6 seconds
94
+ Add-Member + PSObject.TypeNames.Insert: ~3 seconds
95
+
96
+ Initial code borrowed from Shay Levy:
97
+ http://blogs.microsoft.co.il/scriptfanatic/2012/04/13/custom-objects-default-display-in-powershell-30/
98
+
99
+ . LINK
100
+ http://ramblingcookiemonster.github.io/Decorating-Objects/
101
+
102
+ . FUNCTIONALITY
103
+ PowerShell Language
104
+ #>
105
+ [CmdletBinding ()]
106
+ param (
107
+ [Parameter ( Mandatory = $true ,
108
+ Position = 0 ,
109
+ ValueFromPipeline = $true )]
110
+ [ValidateNotNullOrEmpty ()]
111
+ [psobject []]$InputObject ,
112
+
113
+ [Parameter ( Mandatory = $false ,
114
+ Position = 1 )]
115
+ [string ]$TypeName ,
116
+
117
+ [Parameter ( Mandatory = $false ,
118
+ Position = 2 )]
119
+ [System.Collections.Hashtable ]$PropertyToAdd ,
120
+
121
+ [Parameter ( Mandatory = $false ,
122
+ Position = 3 )]
123
+ [ValidateNotNullOrEmpty ()]
124
+ [Alias (' dp' )]
125
+ [System.String []]$DefaultProperties ,
126
+
127
+ [boolean ]$Passthru = $True
128
+ )
129
+
130
+ Begin
131
+ {
132
+ if ($PSBoundParameters.ContainsKey (' DefaultProperties' ))
133
+ {
134
+ # define a subset of properties
135
+ $ddps = New-Object System.Management.Automation.PSPropertySet DefaultDisplayPropertySet, $DefaultProperties
136
+ $PSStandardMembers = [System.Management.Automation.PSMemberInfo []]$ddps
137
+ }
138
+ }
139
+ Process
140
+ {
141
+ foreach ($Object in $InputObject )
142
+ {
143
+ switch ($PSBoundParameters.Keys )
144
+ {
145
+ ' PropertyToAdd'
146
+ {
147
+ foreach ($Key in $PropertyToAdd.Keys )
148
+ {
149
+ # Add some noteproperties. Slightly faster than Add-Member.
150
+ $Object.PSObject.Properties.Add ( ( New-Object System.Management.Automation.PSNoteProperty($Key , $PropertyToAdd [$Key ]) ) )
151
+ }
152
+ }
153
+ ' TypeName'
154
+ {
155
+ # Add specified type
156
+ [void ]$Object.PSObject.TypeNames.Insert (0 , $TypeName )
157
+ }
158
+ ' DefaultProperties'
159
+ {
160
+ # Attach default display property set
161
+ Add-Member - InputObject $Object - MemberType MemberSet - Name PSStandardMembers - Value $PSStandardMembers
162
+ }
163
+ }
164
+ if ($Passthru )
165
+ {
166
+ $Object
167
+ }
168
+ }
169
+ }
170
+ }
0 commit comments