1+ function New-PSMDFormatTableDefinition
2+ {
3+ <#
4+ . SYNOPSIS
5+ Generates a format XML for the input type.
6+
7+ . DESCRIPTION
8+ Generates a format XML for the input type.
9+ Currently, only tables are supported.
10+
11+ Note:
12+ Loading format files has a measureable impact on module import PER FILE.
13+ For the sake of performance, you should only generate a single file for an entire module.
14+
15+ . PARAMETER InputObject
16+ The object that will be used to generate the format XML for.
17+ Will not duplicate its work if multiple object of the same type are passed.
18+
19+ . PARAMETER IncludeProperty
20+ Only properties in this list will be included.
21+
22+ . PARAMETER ExcludeProperty
23+ Only properties not in this list will be included.
24+
25+ . PARAMETER Fragment
26+ The function will only return a partial Format-XML object.
27+ This is useful when combining multiple objects into a single file:
28+ Simply create a full format XML for the first type, then use fragments on the subsequent ones and paste them into the first one's structure.
29+
30+ . EXAMPLE
31+ PS C:\> Get-ChildItem | New-PSMDFormatTableDefinition
32+
33+ Generates a format xml for the objects in the current path (files and folders in most cases)
34+
35+ . EXAMPLE
36+ PS C:\> Get-ChildItem | New-PSMDFormatTableDefinition -IncludeProperty LastWriteTime, FullName
37+
38+ Creates a format xml that only includes the columns LastWriteTime, FullName
39+ #>
40+ [CmdletBinding ()]
41+ Param (
42+ [Parameter (Mandatory = $true , ValueFromPipeline = $true )]
43+ $InputObject ,
44+
45+ [string []]
46+ $IncludeProperty ,
47+
48+ [string []]
49+ $ExcludeProperty ,
50+
51+ [switch ]
52+ $Fragment
53+ )
54+
55+ begin
56+ {
57+ $typeNames = @ ()
58+
59+ # region Xml Definitions
60+ $xmlFull = @"
61+ <?xml version="1.0" encoding="utf-16"?>
62+ <Configuration>
63+ <ViewDefinitions>
64+ <!-- %TypeName% -->
65+ <View>
66+ <Name>%TypeName%</Name>
67+ <ViewSelectedBy>
68+ <TypeName>%TypeName%</TypeName>
69+ </ViewSelectedBy>
70+ <TableControl>
71+ <AutoSize/>
72+ <TableHeaders>
73+ %Headers%
74+ </TableHeaders>
75+ <TableRowEntries>
76+ <TableRowEntry>
77+ <TableColumnItems>
78+ %Columns%
79+ </TableColumnItems>
80+ </TableRowEntry>
81+ </TableRowEntries>
82+ </TableControl>
83+ </View>
84+ </ViewDefinitions>
85+ <Configuration>
86+ "@
87+ $xmlFragment = @"
88+ <!-- %TypeName% -->
89+ <View>
90+ <Name>%TypeName%</Name>
91+ <ViewSelectedBy>
92+ <TypeName>%TypeName%</TypeName>
93+ </ViewSelectedBy>
94+ <TableControl>
95+ <AutoSize/>
96+ <TableHeaders>
97+ %Headers%
98+ </TableHeaders>
99+ <TableRowEntries>
100+ <TableRowEntry>
101+ <TableColumnItems>
102+ %Columns%
103+ </TableColumnItems>
104+ </TableRowEntry>
105+ </TableRowEntries>
106+ </TableControl>
107+ </View>
108+ "@
109+ $xmlColumnHeaderItem = " <TableColumnHeader/>"
110+ $xmlColumnItem = @"
111+ <TableColumnItem>
112+ <PropertyName>%PropertyName%</PropertyName>
113+ </TableColumnItem>
114+ "@
115+ # endregion Xml Definitions
116+ }
117+ process
118+ {
119+ foreach ($object in $InputObject )
120+ {
121+ if ($object.PSObject.TypeNames [0 ] -in $typeNames ) { continue }
122+ else { $typeNames += $object.PSObject.TypeNames [0 ] }
123+
124+ $typeName = $object.PSObject.TypeNames [0 ]
125+ $propertyNames = $object.PSOBject.Properties.Name
126+
127+ if ($IncludeProperty )
128+ {
129+ $propertyNames = $propertyNames | Where-Object { $_ -in $IncludeProperty }
130+ }
131+
132+ if ($ExcludeProperty )
133+ {
134+ $propertyNames = $propertyNames | Where-Object { $_ -notin $ExcludeProperty }
135+ }
136+
137+ if ($Fragment ) { $baseXml = $xmlFragment }
138+ else { $baseXml = $xmlFull }
139+
140+ $baseHeader = ( , $xmlColumnHeaderItem * $propertyNames.Count ) -join ([System.Environment ]::NewLine)
141+
142+ $columns = @ ()
143+ foreach ($property in $propertyNames )
144+ {
145+ $columns += $xmlColumnItem -replace " %PropertyName%" , $property
146+ }
147+ $baseColumn = $columns -join ([System.Environment ]::NewLine)
148+
149+ $baseXml -replace " %TypeName%" , " $typeName " -replace " %Headers%" , $baseHeader -replace " %Columns%" , $baseColumn
150+ }
151+ }
152+ end
153+ {
154+
155+ }
156+ }
0 commit comments