A PowerShell function to replace tokens in the specified template.
function Replace-Token
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseApprovedVerbs', '', Justification = 'Acceptable for its operation')]
[CmdletBinding()]
[OutputType([String])]
param (
[Parameter(Mandatory)]
[string]
$Template,
[Parameter(Mandatory)]
[hashtable]
$Tokens
)
$mustachePattern = '\{\{\s*([^}\s]+)\s*\}\}' # handlebars/mustache pattern, e.g. {{ VARIABLE }}
$matched = [Regex]::Matches($Template, $mustachePattern)
foreach ($match in $matched)
{
$varName = $match.Groups[1].Value
if ($Tokens.ContainsKey($varName))
{
$replacement = $Tokens[$varName]
$Template = $Template.Replace($match.Value, $replacement)
}
}
return $Template
}