-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInject-Variables.ps1
More file actions
executable file
·42 lines (35 loc) · 1.09 KB
/
Inject-Variables.ps1
File metadata and controls
executable file
·42 lines (35 loc) · 1.09 KB
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
[CmdletBinding()]
Param(
[string] $ConfigPath = "./Web.Config",
$ConfigPathOut = $ConfigPath
)
function CheckPath {
if (!(Test-Path $ConfigPath)){
Throw ("Parameter -ConfigPath {0} does not exist" -f $ConfigPath)
}
}
function LoadConfig {
return Get-Content $ConfigPath
}
function ReplaceVariables {
param($config, $envvariables)
Foreach ($ev in $envvariables){
$name = $ev.Name
$value = $ev.Value
$config = $config.replace("{{$name}}", $value)
}
return $config
}
function CheckForMissedTokens {
param($config)
$missedTokens = (Select-String -InputObject $config -Pattern "(?<={{)[^}]*(?=}})" -AllMatches | % {$_.Matches} | % {$_.Value })
echo $missedTokens
Foreach ($missedToken in $missedTokens){
Throw ("Variable '{0}' in {1} that has not been replaced" -f $missedToken,$ConfigPath)
}
}
CheckPath
$config = LoadConfig
$replacedConfig = ReplaceVariables $config (Get-ChildItem Env:)
CheckForMissedTokens $replacedConfig
Set-Content -Path $ConfigPathOut -Value $replacedConfig