Reputation: 11
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 :)
Upvotes: 1
Views: 545
Reputation: 9133
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:
This is the Replaced File Content:
My Input was 192.168.1.1
Upvotes: 1