1

I'm new in Powershell, my code doesn't work, I can't figure out why despite spending hours trying..

What I would like to do: 1) Ask for user to type an IP Address 2) Find and replace a text by this IP Address

My variables

$demande_server = Read-Host "IP Address" >> c:\temp\SQLtemp.txt

$address = Get-Content -Path "c:\temp\SQLtemp.txt"

Then

(Get-Content -path c:\temp\SQL.txt) | foreach {$_ -replace("NomServeur=","NomServeur="$address")} >> c:\temp\SQL.txt

Thank you for your help :)

2
  • there a lot of errors. Syntactically also and by logic also. You are not at all setting the content back actually. Doing input redirection wont help there. Commented Apr 30, 2017 at 4:06
  • Thank you for pointing me on the right direction ! Commented Apr 30, 2017 at 10:29

1 Answer 1

1

Try this logic:

$IPAddress=Read-Host "Enter the IP Address: "
$file_Content= Get-Content "c:\temp\SQLtemp.txt"
foreach($file in $file_Content)
{
$file.replace("NomServeur=","NomServeur=$IPAddress") >> c:\temp\new_OUTPUT.txt
}

So, "new_OUTPUT.txt" will have the new output which should have the replaced text.

Note: If you want to set it back to the old file. Then store the entire result in a variable and then overwrite the old file with the entire result set.

Hope it helps.

Sample OUTPUT:

This is my file content:

File_Content

This is the Replaced File Content:

Replaced Content

My Input was 192.168.1.1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.