1

I am trying to build a PowerShell script such that I give it an input file and regex, it replaces the matching content with the environment variable.

For example, if the input file contains the following:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/Services" xmlns="http://schemas.microsoft.com/2011/01/fabric">
   <Parameters>
      <Parameter Name="IntegrationManager_PartitionCount" Value="1" />
      <Parameter Name="IntegrationManager_MinReplicaSetSize" Value="2" />
      <Parameter Name="IntegrationManager_TargetReplicaSetSize" Value="#{INT_MGR_IC}" />
      <Parameter Name="EventManager_InstanceCount" Value="#{EVT_MGR_IC}" />
      <Parameter Name="Entities_InstanceCount" Value="#{ENT_IC}" />
      <Parameter Name="Profile_InstanceCount" Value="#{PRF_IC}" />
      <Parameter Name="Identity_InstanceCount" Value="#{IDNT_IC}" />
   </Parameters>
</Application>

I would like to build a script that replaces #{INT_MGR_IC} with the value of the INT_MGR_IC environment variable and so on.

Specifically, I am interested to know how to:

  1. Extract and loop over keys from the file such as: #{INT_MGR_IC}, #{EVT_MGR_IC}, etc.
  2. Once I have the key, how do I replace it with an associated environment variable. For example, #{INT_MGR_IC} with INT_MGR_IC env. variable.

Update

This is the RegEx I am planning to use: /#{(.+)}/g

halfer
  • 19,824
  • 17
  • 99
  • 186
Moon
  • 33,439
  • 20
  • 81
  • 132
  • What you mean loop over keys? loop over each parameter? – Martin Brandl May 31 '16 at 19:20
  • @jisaak: Correct, 1. Extract all the strings that are enclosed within `#{` and `}` ... and then replace them with associated ENV variable. – Moon May 31 '16 at 19:22
  • OK guys, before you start voting to close the question, please ask me if you are having difficulties understanding the question! I think this is a valid question. At least, this is the valid problem I am trying to solve at work. I am sure someone, somewhere will also benefit -- especially when it comes to config file transformations in the automated build/deploy process. – Moon May 31 '16 at 19:24
  • Okay, I will try to provide you are script. But not using regex, read why: http://stackoverflow.com/a/1732454/1163423 – Martin Brandl May 31 '16 at 19:24
  • @jisaak: Thanks a lot for the quick response and link. I am going through it. Appreciate it :) – Moon May 31 '16 at 19:26

2 Answers2

2

Just load the file using the Get-Content cmdlet, iterate over each Parmeter, filter all parameter that Value starts with an #using Where-Object and change the value. Finally, use the Set-Content cmdlet to write it back:

$contentPath = 'Your_Path_Here'
$content = [xml] (Get-Content $contentPath)
$content.DocumentElement.Parameters.Parameter | Where Value -Match '^#' | ForEach-Object {
    $_.Value = "REPLACE HERE"
}
$content | Set-Content $contentPath

In case you need to determine an environment variable, you could use [Environment]::GetEnvironmentVariable($_.Value).

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
0

Thanks a lot for everyone who helped. Especially, @jisaak :)


Here is the final script I built that solved the problem in the question. Hopefully its useful to someone!

$configPath = 'Cloud.xml'
$config = [xml] (Get-Content $configPath)
$config.DocumentElement.Parameters.Parameter |  Where {$_.Value.StartsWith("#{")} | ForEach-Object {
    $var = $_.Value.replace("#{","").replace("}","")
    $val = (get-item env:$var).Value
    Write-Host "Replacing $var with $val"
    $_.Value = $val
}

$config.Save($configPath)
Moon
  • 33,439
  • 20
  • 81
  • 132