Repository navigation
Expand file tree
/
Copy pathNew-PSMDFormatTableDefinition.ps1
More file actions
247 lines (202 loc) · 7.72 KB
/
Copy pathNew-PSMDFormatTableDefinition.ps1
File metadata and controls
247 lines (202 loc) · 7.72 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
function New-PSMDFormatTableDefinition
{
<#
.SYNOPSIS
Generates a format XML for the input type.
.DESCRIPTION
Generates a format XML for the input type.
Currently, only tables are supported.
Note:
Loading format files has a measureable impact on module import PER FILE.
For the sake of performance, you should only generate a single file for an entire module.
You can generate all items in a single call (which will probably be messy on many types at a time)
Or you can use the -Fragment parameter to create individual fragments, and combine them by passing
those items again to this command (the final time without the -Fragment parameter).
.PARAMETER InputObject
The object that will be used to generate the format XML for.
Will not duplicate its work if multiple object of the same type are passed.
Accepts objects generated when using the -Fragment parameter, combining them into a single document.
.PARAMETER IncludeProperty
Only properties in this list will be included.
.PARAMETER ExcludeProperty
Only properties not in this list will be included.
.PARAMETER IncludePropertyAttribute
Only properties that have the specified attribute will be included.
.PARAMETER ExcludePropertyAttribute
Only properties that do NOT have the specified attribute will be included.
.PARAMETER Fragment
The function will only return a partial Format-XML object (an individual table definition per type).
.PARAMETER DocumentName
Adds a name to the document generated.
Purely cosmetic.
.PARAMETER SortColumns
Enabling this will cause the command to sort columns alphabetically.
Explicit order styles take precedence over alphabetic sorting.
.PARAMETER ColumnOrder
Specify a list of properties in the order they should appear.
For properties with labels: Labels take precedence over selected propertyname.
.PARAMETER ColumnOrderHash
Allows explicitly defining the order of columns on a per-type basis.
These hashtables need to have two properties:
- Type: The name of the type it applies to (e.g.: "System.IO.FileInfo")
- Properties: The list of properties in the order they should appear.
Example: @{ Type = "System.IO.FileInfo"; Properties = "Name", "Length", "LastWriteTime" }
This parameter takes precedence over ColumnOrder in instances where the
processed typename is explicitly listed in a hashtable.
.PARAMETER ColumnTransformations
A complex parameter that allows customizing columns or even adding new ones.
This parameter accepts a hashtable that can ...
- Set column width
- Set column alignment
- Add a script column
- Assign a label to a column
It can be targeted by typename as well as propertyname. Possible properties (others will be ignored):
Content | Type | Possible Hashtable Keys
Filter: Typename | string | T / Type / TypeName / FilterViewName
Filter: Property | string | C / Column / Name / P / Property / PropertyName
Append | bool | A / Append
ScriptBlock | script | S / Script / ScriptBlock
Label | string | L / Label
Width | int | W / Width
Alignment | string | Align / Alignment
Notes:
- Append needs to be specified if a new column should be added if no property to override was found.
Use this to add a completely new column with a ScriptBlock.
- Alignment: Expects a string, can be any choice of "Left", "Center", "Right"
Example:
$transform = @{
Type = "System.IO.FileInfo"
Append = $true
Script = { "{0} | {1}" -f $_.Extension, $_.Length }
Label = "Ext.Length"
Align = "Left"
}
.EXAMPLE
PS C:\> Get-ChildItem | New-PSMDFormatTableDefinition
Generates a format xml for the objects in the current path (files and folders in most cases)
.EXAMPLE
PS C:\> Get-ChildItem | New-PSMDFormatTableDefinition -IncludeProperty LastWriteTime, FullName
Creates a format xml that only includes the columns LastWriteTime, FullName
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
[OutputType([PSModuleDevelopment.Format.Document], ParameterSetName = "default")]
[OutputType([PSModuleDevelopment.Format.TableDefinition], ParameterSetName = "fragment")]
[CmdletBinding(DefaultParameterSetName = "default")]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$InputObject,
[string[]]
$IncludeProperty,
[string[]]
$ExcludeProperty,
[string]
$IncludePropertyAttribute,
[string]
$ExcludePropertyAttribute,
[Parameter(ParameterSetName = "fragment")]
[switch]
$Fragment,
[Parameter(ParameterSetName = "default")]
[string]
$DocumentName,
[switch]
$SortColumns,
[string[]]
$ColumnOrder,
[hashtable[]]
$ColumnOrderHash,
[PSModuleDevelopment.Format.ColumnTransformation[]]
$ColumnTransformations
)
begin
{
$typeNames = @()
$document = New-Object PSModuleDevelopment.Format.Document
$document.Name = $DocumentName
}
process
{
foreach ($object in $InputObject)
{
#region Input Type Processing
if ($object -is [PSModuleDevelopment.Format.TableDefinition])
{
if ($Fragment)
{
$object
continue
}
else
{
$document.Views.Add($object)
continue
}
}
if ($object.PSObject.TypeNames[0] -in $typeNames) { continue }
else { $typeNames += $object.PSObject.TypeNames[0] }
$typeName = $object.PSObject.TypeNames[0]
#endregion Input Type Processing
#region Process Properties
$propertyNames = $object.PSOBject.Properties.Name
if ($IncludeProperty)
{
$propertyNames = $propertyNames | Where-Object { $_ -in $IncludeProperty }
}
if ($ExcludeProperty)
{
$propertyNames = $propertyNames | Where-Object { $_ -notin $ExcludeProperty }
}
if ($IncludePropertyAttribute)
{
$listToInclude = @()
$object.GetType().GetMembers([System.Reflection.BindingFlags]("FlattenHierarchy, Public, Instance")) | Where-Object { ($_.MemberType -match "Property|Field") -and ($_.CustomAttributes.AttributeType.Name -like $IncludePropertyAttribute) } | ForEach-Object { $listToInclude += $_.Name }
$propertyNames = $propertyNames | Where-Object { $_ -in $listToInclude }
}
if ($ExcludePropertyAttribute)
{
$listToExclude = @()
$object.GetType().GetMembers([System.Reflection.BindingFlags]("FlattenHierarchy, Public, Instance")) | Where-Object { ($_.MemberType -match "Property|Field") -and ($_.CustomAttributes.AttributeType.Name -like $ExcludePropertyAttribute) } | ForEach-Object { $listToExclude += $_.Name }
$propertyNames = $propertyNames | Where-Object { $_ -notin $listToExclude }
}
$table = New-Object PSModuleDevelopment.Format.TableDefinition
$table.Name = $typeName
$table.ViewSelectedByType = $typeName
foreach ($name in $propertyNames)
{
$column = New-Object PSModuleDevelopment.Format.Column
$column.PropertyName = $name
$table.Columns.Add($column)
}
foreach ($transform in $ColumnTransformations)
{
$table.TransformColumn($transform)
}
#endregion Process Properties
#region Sort Columns
if ($SortColumns) { $table.Columns.Sort() }
$appliedOrder = $false
foreach ($item in $ColumnOrderHash)
{
if (($item.Type -eq $typeName) -and ($item.Properties -as [string[]]))
{
[string[]]$props = $item.Properties
$table.SetColumnOrder($props)
$appliedOrder = $true
}
}
if ((-not $appliedOrder) -and ($ColumnOrder))
{
$table.SetColumnOrder($ColumnOrder)
}
#endregion Sort Columns
$document.Views.Add($table)
if ($Fragment) { $table }
}
}
end
{
$document.Views.Sort()
if (-not $Fragment) { $document }
}
}