PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 20, 2022

[FIXED] How to replace a line in a file on multiple computers using PowerShell

 April 20, 2022     connection, powershell, script, windows     No comments   

Issue

I'm trying to replace one line in a test.ini file on multiple PCs with Powershell. But I need to replace this line in a test.ini file with different content. For example:

  • PC st1: in test.ini file line 10 I have "hello" and I need to replace that with "hi"
  • PC st2: in test.ini file line 10 I have "hello" and I need to replace that with "bye"

I created a script but don't understand how can I change the same line with different content on multiple PCs (

 $devices = Get-Content "C:\script\device.txt"

foreach ($computer in $devices) {

Invoke-Command -ComputerName $computer -scriptblock {
((Get-Content -path C:\test\test.ini -Raw) -replace 'hello','world') | Set-Content -Path C:\test\test.ini
Get-Content -path C:\test\test.ini | Select -Index 10 }

}

Please help if I can do that perhaps.


Solution

Ok, so here's what I mean.

Instead of the text file with just the computer names in it, make that a CSV file like user2670623 already commented

ComputerName,SearchWord,Replacement
C2712,hello,hi
C1278,hello,bye
C2452,hello,again

Now you have the computer name, the search word AND the replacement for that particular computer all in one and then you can do stuff like this:

$devices = Import-Csv "C:\script\device.csv"

foreach ($computer in $devices) {
    Invoke-Command -ComputerName $computer.ComputerName -ScriptBlock {
        param(
            [string]$findThis,
            [string]$replaceWith
        )
        # -replace uses regex, so the $findThis string needs to be escaped because it may or may not
        # contain characters that have special meaning in Regular Expressions.
        (Get-Content -Path 'C:\test\test.ini' -Raw) -replace [regex]::Escape($findThis), $replaceWith | Set-Content -Path 'C:\test\test.ini'
    } -ArgumentList $computer.SearchWord $computer.Replacement
}

If it is not possible to create the CSV file for some reason, then you're stuck with tedious code like

$devices = Get-Content "C:\script\device.txt"

foreach ($computer in $devices) {
    # for each computer, define what is to be replaced by what
    switch ($computer) {
        'C2712' { $find = 'hello'; $replace = 'hi' }
        'C1278' { $find = 'hello'; $replace = 'bye' }
        'C2452' { $find = 'hello'; $replace = 'again' }
        # etcetera etcetera..
    }
    Invoke-Command -ComputerName $computer -ScriptBlock {
        # -replace uses regex, so the $findThis string needs to be escaped because it may or may not
        # contain characters that have special meaning in Regular Expressions.
        (Get-Content -Path 'C:\test\test.ini' -Raw) -replace [regex]::Escape($using:find), $using:replace | Set-Content -Path 'C:\test\test.ini'
    }
}


Answered By - Theo
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing