11function Get-PSMDTemplate
22{
3- [CmdletBinding ()]
3+ <#
4+ . SYNOPSIS
5+ Search for templates to create from.
6+
7+ . DESCRIPTION
8+ Search for templates to create from.
9+
10+ . PARAMETER TemplateName
11+ The name of the template to search for.
12+ Templates are filtered by this using wildcard comparison.
13+ Defaults to "*" (everything).
14+
15+ . PARAMETER Store
16+ The template store to retrieve tempaltes from.
17+ By default, all stores are queried.
18+
19+ . PARAMETER Path
20+ Instead of a registered store, look in this path for templates.
21+
22+ . PARAMETER Tags
23+ Only return templates with the following tags.
24+
25+ . PARAMETER Author
26+ Only return templates by this author.
27+
28+ . PARAMETER MinimumVersion
29+ Only return templates with at least this version.
30+
31+ . PARAMETER RequiredVersion
32+ Only return templates with exactly this version.
33+
34+ . PARAMETER All
35+ Return all versions found.
36+ By default, only the latest matching version of a template will be returned.
37+
38+ . PARAMETER EnableException
39+ Replaces user friendly yellow warnings with bloody red exceptions of doom!
40+ Use this if you want the function to throw terminating errors you want to catch.
41+
42+ . EXAMPLE
43+ PS C:\> Get-PSMDTemplate
44+
45+ Returns all templates
46+
47+ . EXAMPLE
48+ PS C:\> Get-PSMDTemplate -TemplateName module
49+
50+ Returns the latest version of the template named module.
51+ #>
52+ [CmdletBinding (DefaultParameterSetName = ' Store' )]
453 Param (
54+ [Parameter (Position = 0 )]
55+ [string ]
56+ $TemplateName = " *" ,
57+
58+ [Parameter (ParameterSetName = ' Store' )]
559 [string ]
6- $TemplateName
60+ $Store = " *" ,
61+
62+ [Parameter (Mandatory = $true , ParameterSetName = ' Path' )]
63+ [string ]
64+ $Path ,
65+
66+ [string []]
67+ $Tags ,
68+
69+ [string ]
70+ $Author ,
71+
72+ [version ]
73+ $MinimumVersion ,
74+
75+ [version ]
76+ $RequiredVersion ,
77+
78+ [switch ]
79+ $All ,
80+
81+ [switch ]
82+ $EnableException
783 )
884
985 begin
1086 {
11- Write-PSFMessage - Level InternalComment - Message " Bound parameters: $ ( $PSBoundParameters.Keys -join " , " ) " - Tag ' debug' , ' start' , ' param'
87+ Write-PSFMessage - Level InternalComment - Message " Bound parameters: $ ( $PSBoundParameters.Keys -join " , " ) " - Tag ' debug' , ' start' , ' param'
88+
89+ $prospects = @ ()
1290 }
1391 process
1492 {
15-
93+ # region Scan folders
94+ if (Test-PSFParameterBinding - ParameterName " Path" )
95+ {
96+ $templateInfos = Get-ChildItem - Path $Path - Filter " $ ( $TemplateName ) -*.Info.xml" | Where-Object { ($_.Name -replace " -\d+(\.\d+){0,3}.Info.xml$" ) -like $TemplateName }
97+
98+ foreach ($info in $templateInfos )
99+ {
100+ $data = Import-Clixml $info.FullName
101+ $data.Path = $info.FullName -replace ' \.Info\.xml$' , ' .xml'
102+ $prospects += $data
103+ }
104+ }
105+
106+ else
107+ {
108+ $stores = Get-PsmdTemplateStore - Filter $Store
109+
110+ foreach ($item in $stores )
111+ {
112+ if ($item.Ensure ())
113+ {
114+ $templateInfos = Get-ChildItem - Path $item.Path - Filter " $ ( $TemplateName ) -*.Info.xml" | Where-Object { ($_.Name -replace " -\d+(\.\d+){0,3}.Info.xml$" ) -like $TemplateName }
115+
116+ foreach ($info in $templateInfos )
117+ {
118+ $data = Import-Clixml $info.FullName
119+ $data.Path = $info.FullName -replace ' \.Info\.xml$' , ' .xml'
120+ $data.Store = $item.Name
121+ $prospects += $data
122+ }
123+ }
124+ # If the user asked for a specific store, it should error out on him
125+ elseif ($item.Name -eq $Store )
126+ {
127+ Stop-PSFFunction - Message " Could not find store $Store " - EnableException $EnableException - Category OpenError - Tag ' fail' , ' template' , ' store' , ' open'
128+ return
129+ }
130+ }
131+ }
132+ # endregion Scan folders
16133 }
17134 end
18135 {
19-
136+ $filteredProspects = @ ()
137+
138+ # region Apply filters
139+ foreach ($prospect in $prospects )
140+ {
141+ if ($Author )
142+ {
143+ if ($prospect.Author -notlike $Author ) { continue }
144+ }
145+ if (Test-PSFParameterBinding - ParameterName MinimumVersion)
146+ {
147+ if ($prospect.Version -lt $MinimumVersion ) { continue }
148+ }
149+ if (Test-PSFParameterBinding - ParameterName RequiredVersion)
150+ {
151+ if ($prospect.Version -ne $RequiredVersion ) { continue }
152+ }
153+ if ($Tags )
154+ {
155+ $test = $false
156+ foreach ($tag in $Tags )
157+ {
158+ if ($prospect.Tags -contains $tag )
159+ {
160+ $test = $true
161+ break
162+ }
163+ }
164+ if (-not $test ) { continue }
165+ }
166+
167+ $filteredProspects += $prospect
168+ }
169+ # endregion Apply filters
170+
171+ # region Return valid templates
172+ if ($All ) { return $filteredProspects }
173+
174+ $prospectHash = @ { }
175+ foreach ($prospect in $filteredProspects )
176+ {
177+ if ($prospectHash.Keys -notcontains $prospect.Name )
178+ {
179+ $prospectHash [$prospect.Name ] = $prospect
180+ }
181+ elseif ($prospectHash [$prospect.Name ].Version -lt $prospect.Version )
182+ {
183+ $prospectHash [$prospect.Name ] = $prospect
184+ }
185+ }
186+ $prospectHash.Values
187+ # endregion Return valid templates
20188 }
21189}
0 commit comments