1

Im trying to write a script in powershell that will add new parameters from updates into a ini file.

They are categorized with [categorie]

It checks what parameters are missing and then i want them to be added under the categorie.

 $inipath = "somepath"
  $list =@([PSCustomObject] @{ type = "\[cat\]"; values = "par1=N","par2=N", "par3=N"})

foreach($entry in $list){
    if(Select-String -Path $inipath -Pattern $entry.values -SimpleMatch -Quiet){
    }
    else{
        $parameter += @([PSCustomObject] @{ type = $entry.type; values =` $entry.values })
   }
}

$tlength = $parameter.type.length
    for($x=0; $x -le $tlength; $x++ ){
        $vlength = $parameter[$x].values.Length
        for($y=0;$y -le $vlength; $y++){
            $par = $parameter[$x].values[$y]
            $par = $par -join '`n'
    $fileContent = Get-Content $inipath
    $linenumber= Get-Content $inipath | select-string $parameter[$x].type
    $fileContent[$lineNumber.LineNumber +1] += $par
    $fileContent | Set-Content $inipath  
    
    }

Right now it recognizes the missing parameters and prints them but its printing them like this:

[cat]par1=Npar2=N=par3=N

Desired out put would be

[cat]
par1=N
par2=N
par3=N
  • When manipulating Ini files, it is better to use a dedicated module like [PsIni](https://www.powershellgallery.com/packages/PsIni/3.1.2) – Theo Sep 12 '22 at 11:42

2 Answers2

0

In PowerShell, strings wrapped in single quotes are taken literally, while strings wrapped in double quotes are interpreted and processed for variables, etc.

You need to change:

$par = $par -join '`n'

to:

$par = $par -join "`n"
Richard Dunn
  • 6,165
  • 1
  • 25
  • 36
  • Windows uses ```"`r`n"```, so better use `[Environment]::NewLine` – Theo Sep 12 '22 at 11:44
  • Not _my_ Windows. Isn't that half the point of that variable, to abstract the underlying OS? – Richard Dunn Sep 12 '22 at 11:48
  • @Theo, see [this answer](https://stackoverflow.com/questions/60971765/what-is-the-difference-between-rn-and-n-for-line-breaks-in-powershell) – Richard Dunn Sep 12 '22 at 11:51
  • Ah? Then on your Windows `([Environment]::NewLine).Length` returns `1` ? What windows is that then? – Theo Sep 12 '22 at 11:51
  • It's 2, but it's not literally mapped to that, whether you use rn or n PowerShell will convert to whatever [Environment]::NewLine is. They're interchangeable. – Richard Dunn Sep 12 '22 at 11:54
  • Unfortunately, even after changing it to "`n" its still not working. Still printing [cat]par1=Npar2=N=par3=N – jesse North Sep 12 '22 at 12:16
0

In the end i fixed it by replacing the function with a function that replaces the entire [category] with [category] + "`n" + $param

    $tlength = $parameter.type.length
    for($x=0; $x -le $tlength; $x++ ){
        $vlength = $parameter[$x].values.Length
        for($y=0;$y -le $vlength; $y++){
            if(!$vlength){
                echo "no parameters"
            }
            elseif(!$parameter[$x].values[$y]){
                echo "no parameters"
            }
            else{
            $par = $parameter.type[$x]
            $a = $parameter.type[$x] + "`n" + $parameter[$x].values[$y]
            $a = $a -replace '\\',''
            echo $a
    (Get-Content $inipath) -replace $par, $a | Set-Content $inipath  
            }
    }
}

now it prints

[cat]
param1=n
param2=n