Multiple ways of PowerShell templating
From time to time everyone faces this. You need templates - either HTML, email or just a couple of strings, but anyway… Here I’ve selected several solutions that may help you. If you know any other good solutions, feel free to mention them in comments - I’ll add them to this post.
ExpandString - fast and dirty
$Name = "Dude"
$ExecutionContext.InvokeCommand.ExpandString('Hello ${Name}!!!');
You can use PowerShell execution context on a string to replace variables with their values. It wil actually pass the string through PowerShell interpreter, so you can use any PowerShell syntax inside. The main problems are:
- You must prevent arbitraty code execution, because ExecutionContext supports
$(...)
- Context has access to all variables within execution scope. So the better way is to prepare the variables and call the context in a separate function.
String.Replace and Mustache templates
Yes, just replace in a string. Look at the code, it’s really simple:
function Merge-Tokens($template, $tokens)
{
return [regex]::Replace(
$template,
'\$\{(?<tokenName>\w+)\}\$',
{
param($match)
$tokenName = $match.Groups['tokenName'].Value
return $tokens[$tokenName]
})
}
There are lots of implementations: PSTokens and lots of GitHub gists.
Mustache
There’s a very good and straightforward standard called [mustache]{https://mustache.github.io/} that covers around 90% of all “templater” needs. Though it’s not officially mentioned, there are some PowerShell implementations, particularly Postache (based on .NET implementation) and BladePS - not really full implementation, but still works. I’d personally recommend Postache.
EPS - ERB-like templates
So, here’s the monster - EPS. It’s based on Ruby ERB templates ideas and internally executes PowerShell code in templates. Just read the Readme - it will explain things better than me.
PSStringTemplate
This engine’s implements StringTemplate4 - Java template engine with ports to several other languages. The templater docs can be found here
What to choose
For me, EPS and Mustache are the choices. In simple cases and to make the templates compact, I’ll use Mustache. But to feel all the power - EPS is the best for me.
blog comments powered by Disqus
Published
Category
scriptsTags
2020
August
- August 11, 2020 » Identifying AWS EBS volumes on instance
June
- June 24, 2020 » AWS S3 Website with private-only access
2018
September
- September 4, 2018 » Multiple ways of PowerShell templating
2017
September
- September 17, 2017 » Link: RDP URI scheme
August
- August 15, 2017 » Link: SSL settings and checks
- August 15, 2017 » Link: Template files substituter for Docker
- August 8, 2017 » Link: Classes in PowerShell
April
- April 15, 2017 » Bash tricks you didn't usually use
2016
August
- August 30, 2016 » Configuring RancherOS for use with AWS autoscaling + Swarm cluster
April
- April 26, 2016 » Building JS assets with MSBuild
- April 25, 2016 » Applying Web.config transforms to all config files
2015
August
- August 29, 2015 » Check if identifier is declared in Bash
2014
April
- April 2, 2014 » PID file management in Bash
- April 2, 2014 » Logging routine for Bash
- April 2, 2014 » Lock file management in Bash