-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSTemplate.psm1
56 lines (48 loc) · 1.56 KB
/
PSTemplate.psm1
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
<#
.synopsis
- Fills out a template filled with {{name}} variables using a hash table
.description
- Template should be formatted as a string, with {{<VariableName>}} used as the tokens to replace
- Replacements should be in a hash table as <VariableName> = ReplacementValue
.example
- Fill-Template -Template "simple {{Type}} but could be a here-string" -Variables @{'Type' = 'string'}
- returns: simple string but could be a here-string
.parameter Template
- the template in the form of a string
.parameter Variables
- the variable key-value pairs to replace
.outputs
- returns the input Template form with the replacements applied
.notes
Author: Ben Renninson
Email: ben@goldensyrupgames.com
From: https://github.com/GSGBen/PSTemplate
#>
function Invoke-PSTemplate
{
Param
(
[Parameter(Position=0,ValueFromPipeline=$true)][string[]]$Template,
[Parameter(Position=1,Mandatory=$true)][System.Collections.IDictionary]$Variables
)
Begin
{
# don't join anything: we want to return the output in the same form as the input
}
Process
{
$Return = $Template
$Variables.GetEnumerator() | Select-Object Name,Value | ForEach-Object {
# this allows "" to go through, but doesn't throw on a fully missing $Template
if ($null -ne $Return)
{
$Return = $Return.Replace("{{$($_.Name)}}",$_.Value)
}
}
return $Return
}
End
{
}
}
New-Alias -Name Fill-Template -Value Invoke-PSTemplate