0

I'm searching to replace 2 occurrences of a specific string but each by one data different.

Here the file with 2 lines :

[SERVER1]

NAME = SERVER1\SQLEXPRESS

ODBCLINK = idms

USER = idms

PSW = idms

[SERVER2]

NAME = SERVER2\SQLEXPRESS

ODBCLINK = backupidms

USER = idms

PSW = idms

For the moment I have that code:

Get-Content ".\test.ini") | ForEach-Object { $_ -replace ".+\SQLEXPRESS" , "Name = $hostname\SQLEXPRESS" } | Set-Content ".\test.ini"

The goal is to have that :

[SERVER1]

NAME = Paris\SQLEXPRESS

ODBCLINK = idms

USER = idms

PSW = idms

[SERVER2]

NAME = Nantes\SQLEXPRESS

ODBCLINK = backupidms

USER = idms

PSW = idms

I read these 2 strings, Paris and Nantes, from another file.

Test1 and TEst2 can be ALieoej and PAodj45p. it's arbitrary choice

I think i need a script to search one line with sqlexpress into, and change it by one data, and the second time where i find it, replace it by another data

m.wahbi
  • 1
  • 3
  • 1
    Possible duplicate of [String replace file content with PowerShell](http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell) – Sean Jun 10 '16 at 13:48
  • Hi Sean, i had already read this post, but i can't resolved my issue :/ – m.wahbi Jun 10 '16 at 13:58
  • Your question is not clear. The example you give does not map well to the description given before it and the title of the question is meaningless. I understand you do not want to change a random string but that is as far as clarity goes. – Martin Maat Jun 10 '16 at 14:13
  • Hi Martin Maat, I will try with my poor english to explain better my goal. – m.wahbi Jun 10 '16 at 14:17
  • As far as I have understood this is: you want to replace every instance of 'NAME = Server1\SQLEXPRESS' in the text file with 'NAME = Test1\SQLEXPRESS'. Where 'Test1' is the value you are getting from '$hostname' variable. Right?. In that case The value in '$hostname' is not changing during the ForEach loop. Hence you are getting same values in output. That was one part. However, for replacing the value that way, you can use: `Get-Content .\test.ini | %{ $_ -replace "Name = .*\SQLExpress", "Name = $hostname\SqlExpress" }` – SavindraSingh Jun 10 '16 at 14:44
  • I had try your solution, SavindraSingh, but this nos respond to my goal. My goal . i will edit my question again to be more clarity – m.wahbi Jun 10 '16 at 14:52
  • If you just want to preserve any trailing digits in the original host names, you could do $_ -replace "(^[A-Z][a-z]+)", $env:computername – Martin Maat Jun 10 '16 at 14:54
  • In fact like i edited, test1 and test2 are a choice arbitrary – m.wahbi Jun 10 '16 at 15:01

2 Answers2

0

Seems like this may be what you are looking for. This will CREATE the test.ini file instead of editing it as you asked.

Set-Content -Value "Test1
Test2" -Path computers.txt

(Get-Content ".\computers.txt") | ForEach-Object {"Name = $_\SQLEXPRESS" } | Set-Content ".\test.ini"

Where computers.txt is your source of computers that have SQL Express instances.

stevlars
  • 96
  • 10
  • i will try it. i will come to give a feeback – m.wahbi Jun 13 '16 at 07:25
  • That can be like that, but n my ini file i have multi line i will edit my first post. – m.wahbi Jun 13 '16 at 07:28
  • Thinking about what problem you are trying to solve... Do you know that there is a mapping between names like SERVER1 (or whatever) will be replaced by Paris (or whatever) on the "NAME = ..." line (wherever it occurs)? Or do you know the first "NAME = ..." line will be Paris (or whatever) with the next "NAME = ..." will be Nantes (or whatever)? Those are different problems. You seem to be describing the second situation. If so, do you know that, for instance, you are matching the correct name to "ODBCLINK = backupidms" vs. "ODBCLINK = idms"? – stevlars Jun 13 '16 at 18:59
  • The first "Name = ....." line will be always Paris, and the second will be always Nantes. – m.wahbi Jun 14 '16 at 08:52
  • in fact , this 2 names (Pairs, and Nantes i recup them from a txt file) – m.wahbi Jun 14 '16 at 08:59
  • Do you care what happens if you have more names in the txt file than "NAME = ..." lines or vise versa? – stevlars Jun 14 '16 at 17:32
  • In fact in this file, i need really juste 2 block of informations within each one time "Name=.." – m.wahbi Jun 15 '16 at 08:01
0

Hi i found myself a solution :

rm $fichierDest
$content = get-content $fichierSource



    $i = 0
    while($i -lt $content.Length - 1)
    {
          $line = $content[$i]
          $line2 = $content[$i+1]
          if($line -match "SERVER1")
          {
                $line2 = $line2 -Replace ".+\SQLEXPRESS" , "Name = TEST1\SQLEXPRESS"
                add-content $fichierDest $line
                add-content $fichierDest $line2
                $i += 2
          }
          elseif($line -match "SERVER2")
          {
                $line2 = $line2 -Replace ".+\SQLEXPRESS" , "Name = TEST2\SQLEXPRESS"
                add-content $fichierDest $line
                add-content $fichierDest $line2
                $i += 2
          }
          else
          {
                add-content $fichierDest $line
                $i++
          }
    }
m.wahbi
  • 1
  • 3