I have a function written in PowerShell:
function replace([string] $name,[scriptblock] $action) {
Write-Host "Replacing $name"
$_ = $name
$action.Invoke()
}
and would be used as:
$name = "tick"
replace $agentPath\conf\buildAgent.dist.properties {
(cat templates\buildAgent.dist.properties.tpl) `
-replace '@@serverurl@@', 'http:/localhost:8080/teamcity' `
-replace '@@name@@', $name `
> $_
}
However I've discovered that within the scriptblock the variable $name
is overwritten by the $name
param from within the replace
function.
Is there a way to execute the script block so that only the variable $_
is added to the scope of the scriptblock but nothing else is?