Changing a parameter in a configuration file via PowerShell.

This topic contains 5 replies, has 3 voices, and was last updated by  Mj Ross 1 year, 9 months ago.

  • Author
  • #33259

    Mj Ross

    What am I doing wrong here? I just need to replace the "shared_buffers = 32MB value to 256MB in a configuration file(s)?

    (Get-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf)
    |ForEach-Object {$_ -replace "shared_buffers = 32MB", "shared_buffers = 256MB"}
    |Set-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf

    Thank you for any help you could offer?

  • #33264


    That code works. When you remove the final pipe and Set-Content cmdlet, does it write the correct data to the screen? My first thought is that you're working with a read-only file; however, that would probably indicate an error. Maybe not, so try the Set-Content cmdlet with the -Force parameter. Speaking of errors, are you seeing any errors; you didn't indicate that one way, or another.

  • #33275

    Mj Ross

    I have removed the | and the script seems to execute but the change never seems to take place. I am getting a mess at the end of the script echo:

    cmdlet set-content at command pipeline position 1
    Supply values for the following parameters:

    it seems if i make an entry – the entire file go to blank.


  • #33277


    The "Value" error at the end is indicating that nothing is being supplied to the Set-Content cmdlet's mandatory -Value parameter. Type Set-Content by itself into the PowerShell console and you'll see what I mean. In fact, you'll see this with any cmdlet, or function, that requires a specific parameter(s) have a value when it's run.

    I have this working with an example file that (at times), both did, and didn't contain 'shared_buffers = 32MB' and it worked. I'm not sure what else to say. Can you post the beginning to end run, so we can see everything? Providing my image shows below, you should be able to see this worked for me.

  • #33284

    Max Kozlov

    Split your code by Pipe, but LEAVE the pipe at the end of line!

    (Get-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf) |
    ForEach-Object {$_ -replace "shared_buffers = 32MB", "shared_buffers = 256MB"} |
    Set-Content F:\hp\UCMDB\DataFlowProbe\pgsql\data\postgresql.conf

  • #33307

    Mj Ross

    The pipe at the END of the line did the trick and the code is working as desired. I can't say thanks enough.

You must be logged in to reply to this topic.