0

I get an element from the array, I check whether the line exists in the file or not. If it exists, then I change it to an element from the second array by index. Everything works fine. The problem is that if a string from an array is similar to a string in a file, then it is considered that it is in the file. For example, "#TokenUri" and "#Token" are treated the same. The "$initial_array" variable is taken from the vault secret store. Added to the code for ease of debugging. All keys and values are a random set of characters, close to reality. It is necessary to look for a strict one-to-one correspondence.

   $pathFile = 'D:\Git\Gitlab\powershell\appsettings.json'
   $array_key = @()
   $array_value = @()
   
   
   $initial_array = "@{#TokenUri=ovnsinv-iovi0ew-dvoiw9; #User=Vasia; #Timeout=00:00:30; #ExternalServices=gdgdfg; #Password=xvnen834n9; #ApplicationName=Tupoe; #Uri=https://gitlab.com/}" -replace '[@,{,},;]'
   $array= $initial_array.Split('=').Split(' ')
   
   for ($i = 0; $i -le $array.Count; $i++) {
       if ( ($i % 2) -eq 0) {
           $array[$i] | ForEach-Object { $array_key+=$_ }
       }
       else {
           $array[$i] | ForEach-Object { $array_value+=$_ }
       }
   }
   
   
   for ($i=0; $i -lt $array_key.Length; $i++) {
       if ((Get-Content -Path $pathFile) -match $array_key[$i]) {
       
           (Get-Content -Path $pathFile) -replace $array_key[$i], $array_value[$i] | Set-Content -Path $pathFile
       
       } 
       else {
           $element = $array_key[$i]
           throw "Element $element is missing from the file - $pathFile"
           break
       }
   } 
  • 2
    it seems like you're looking for `if ((Get-Content -Path $pathFile) -eq $array_key[$i]) {` instead of `if ((Get-Content -Path $pathFile) -match $array_key[$i]) {` though your question is unclear and in need of a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Santiago Squarzon Jul 03 '22 at 18:04
  • It is generally a bad practice to use regular expressions for peeking and poking in structured text. Instead, use the related parsers as in this case the [`ConvertFrom/ConvertTo-Json`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-json) cmdlets. – iRon Jul 03 '22 at 20:09
  • As a side note: [try avoid using the increase assignment operator (`+=`) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponentially expensive. – iRon Jul 03 '22 at 20:11
  • It looks like a script to apply token substitutions to a config file - e.g. a file with the literal contents ```{ "Uri": "#TokenUri" }``` becomes ```{ "Uri" = "ovnsinv-iovi0ew-dvoiw9" }```. The problem is there's no decoration / delimiters for the token placeholders in the template (e.g. ```{ "Uri": "{{#Token}}" }``` or whatever) so if there's tokens called ```#Token``` *and* ```#TokenUri``` then ```#TokenUri``` in the template is ambiguous because it could be substituted as ```{{#TokenUri}}``` *or* ```{{#Token}}Uri``` depending on which token is processed first. – mclayton Jul 03 '22 at 20:21
  • Thank you for your advice. You understood correctly, this is indeed a script for replacing tokens in the configuration file. This line is - {#TokenUri=ovnsinv-iovi0ew-dvoiw9; #User=Vasia; #Timeout=00:00:30; #ExternalServices=gdgdfg; #Password=xvnen834n9; #ApplicationName=Tupoe; #Uri=https://gitlab.com/} is obtained by fetching it from the HashiCorp Vault. After that, you need to replace the keys in the configuration file with values obtained from vault. I didn't quite understand what you meant by saying delimiters in the template – user19473948 Jul 06 '22 at 17:32

0 Answers0