-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathResolve-VariableToken.ps1
45 lines (41 loc) · 1.57 KB
/
Resolve-VariableToken.ps1
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
Function Resolve-VariableToken {
<#
.SYNOPSIS
Replaces all the tokens in a string with provided variables
.DESCRIPTION
Parses input string and replaces all the #{tokens} inside it with provided variables
.PARAMETER InputString
String to parse
.PARAMETER Runtime
Variables collection. Token names should match keys in the hashtable
.EXAMPLE
Resolve-VariableToken -InputString "SELECT '#{foo}' as str" -Runtime @{ foo = 'bar'}
#>
[CmdletBinding()]
Param (
[object[]]$InputObject,
[object]$Runtime,
[string]$TokenRegex = (Get-DBODefaultSetting -Name config.variabletoken -Value)
)
foreach ($obj in $InputObject) {
if ($obj -is [string]) {
$output = $obj
foreach ($token in (Get-VariableToken -InputString $obj -RegexString $TokenRegex.Replace('token', '[a-zA-Z0-9_\-.]+'))) {
#Replace variables found in the config
$tokenRegExString = $TokenRegex.Replace('token', [Regex]::Escape($token))
if ($Runtime) {
if ($Runtime -is [hashtable]) { $variableList = $Runtime.Keys }
else { $variableList = $Runtime.psobject.Properties.Name }
if ($variableList -contains $token) {
Write-PSFMessage -Level Debug -Message "Replacing token $token"
$output = $output -replace $tokenRegExString, $Runtime.$token
}
}
}
$output
}
else {
$obj
}
}
}