I have a file that looks something like this:
<Parameter Name="WebImage" Value="web:${BuildNumber}" />
<Parameter Name="ApiImage" Value="api:${BuildNumber}" />
I'd like to replace any occurrence of ${xxx}
in the file with the environment variable xxx
. This file could refer to any number of environment variables; they are not known ahead of time.
I'm trying something like:
(Get-Content .\Cloud.xml) -replace "\$\{(\w+)\}", "$([Environment]::GetEnvironmentVariable('$1'))"
However, I just get:
<Parameter Name="WebImage" Value="web:" />
<Parameter Name="ApiImage" Value="api:" />
The GetEnvironmentVariable
call works, since I can do:
(Get-Content .\Cloud.xml) -replace "\$\{(\w+)\}", "$([Environment]::GetEnvironmentVariable('BuildNumber'))"
And that works. The $1
call works since I can do:
(Get-Content .\Cloud.xml) -replace "\$\{(\w+)\}", '$1'
And I'll get:
<Parameter Name="WebImage" Value="web:BuildNumber" />
<Parameter Name="ApiImage" Value="api:BuildNumber" />
However, I can't seem to combine the two. I think it's something to do with the order of how functions are resolved.