IP address need to replace in text file

Welcome Forums General PowerShell Q&A IP address need to replace in text file

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
1 day, 5 hours ago.

  • Author
    Posts
  • #176362

    Participant
    Topics: 1
    Replies: 3
    Points: 23
    Rank: Member

    Hi All,

    i have created a script to replace the IP address on the multiple files. Script will get the user Input IP address and find the in the text file and replace an new IP address. When i try to replace the IP address, its not replacing properly with match condition.

    $server = Get-Content "F:\Scripts\Replace_DNS_IP\server.txt"
    $_DNSList = @()
    
    #Get the input from the user
    $_DNSList = New-Object System.Net.IPAddress(0x1521A8C0)
    
    $_DNSList = READ-HOST "Enter the DNS Servers IP address(use , comma for addditional IPs )"
    #$_DNSList = $_DNSListip.Split(',').Split(' ')
    #splitting the list of input as array by Comma & Empty Space
    $_DNSList = $_DNSList.Split(',').Split(' ')
    
    $_ReplaceDNSList = @()
    $_ReplaceDNSList = New-Object System.Net.IPAddress(0x1521A8C0)
    #Get the input from the user
    $_ReplaceDNSList = READ-HOST "Enter the Replace DNS Servers IP address(use , comma for addditional IPs )"
    #$_ReplaceDNSList =$_ReplaceDNSListip.Split(',').Split(' ')
    
    #splitting the list of input as array by Comma & Empty Space
    $_ReplaceDNSList = $_ReplaceDNSList.Split(',').Split(' ')
    
    foreach ($input in $server) {
    
        $LogPath = Get-Item "F:\Scripts\Replace_DNS_IP\PSD Backup\$input.psd1"
    
        $Lines = Get-Content $LogPath | Where-Object {$_ -match "DNSServerAddress"}
    
        Foreach ($Line in $Lines) {
    
            #$IP =@()
            #$ip = New-Object System.Net.IPAddress(0x1521A8C0)
            $IP = ($Line | Select-String -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches).Matches.Value
    
            foreach ($IPs in $IP) {
                if ($IPs -match $_DNSList[0]) {
                    Write-Host "Replacing DNS1"
                    (Get-Content -Path $LogPath).Replace($IPs, $_ReplaceDNSList[0]) | Set-Content -Path $LogPath
                }
                elseif ($IPs -match $_DNSList[1]) {
                    Write-Host "Replacing DNS2"
                    (Get-Content -Path $LogPath).Replace($IPs, $_ReplaceDNSList[1]) | Set-Content -Path $LogPath
                }
                elseif ($IPs -match $_DNSList[2]) {
                    Write-Host "Replacing DNS3"
                    (Get-Content -Path $LogPath).Replace($IPs, $_ReplaceDNSList[2]) | Set-Content -Path $LogPath
                }
                #elseif($IPs -match $_DNSList[3])
                {
                    Write-Host "Replacing DNS4"
                    (Get-Content -Path $LogPath).Replace($IPs, $_ReplaceDNSList[3]) | Set-Content -Path $LogPath
                }#>
                else
                {
                    Write-Host "DNS IP address is not Matching with Input"
                }
            }
        }
    }
    

    Output of the script

    Via the script am replacing 3 IP address in text file. for example Source IP{8.8.8.8,4.4.4.4,5.5.5.5}

    1. if i try to replace 8.8.8.8,5.5.5.5, script will replace the new IP address, but 4.4.4.4 will be changed to blank " "
    2. if i try to replace 4.4.4.4, script is replacing only first number of the IP address"1" inside of "198.166.1.1

    Kindly help me to fix it.

    Thanks & Regards

    Manikandan.K

  • #176734

    Participant
    Topics: 1
    Replies: 3
    Points: 23
    Rank: Member

    Hi All,

    i have fixed the blank space replacement issue. But am facing during Single IP address replacement. If i provide the Single IP address, i was replacing entire IP address on the text file

  • #176836

    Participant
    Topics: 10
    Replies: 59
    Points: 142
    Helping Hand
    Rank: Participant

    Manikandan.K,

    It would help if you could post your revised code. In the meantime, some suggestions for your code:

    1. It is not ideal to use $_ as a variable prefix because it's too close to the $psitem/$_ used in loops/error handing
    2. You'd want to consider refactoring the foreach statement (similar to below) for logic to make more sense and the code easier to read:
      $IPAddresses = ($Line | Select-String -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches).Matches.Value
      # previously $IP = ($Line ...
      
      foreach ($IP in $IPAddresses) {
          ...
      }
    3. Replace all the if/elseif statements with one switch statement:
      # inside foreach ($IP in IPAddresses)
      
      switch ($IP) {
          { $_ -match $_DNSList[0] } {
              Write-Host "Replacing DNS1"
              (Get-Content -Path $LogPath).Replace($_, $_ReplaceDNSList[0]) | Set-Content -Path $LogPath
          }
          { $_ -match $_DNSList[1] } {
              Write-Host "Replacing DNS2"
              (Get-Content -Path $LogPath).Replace($_, $_ReplaceDNSList[1]) | Set-Content -Path $LogPath
          }
          ...
      }
    • #178044

      Participant
      Topics: 1
      Replies: 3
      Points: 23
      Rank: Member

      Hi Aaron,

      Kindly fine the below revised code. And also i will check you code too.

      $server = Get-Content "F:\Scripts\Replace_DNS_IP\server.txt"
      
      $regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
      
      $_DNSList= @()
      
      #Get the input from the user
      $_DNSList = New-Object System.Net.IPAddress(0x1521A8C0)
      $_DNSList = READ-HOST "Enter the DNS Servers IP address(use , comma for addditional IPs )"
      
      #splitting the list of input as array by Comma & Empty Space
      $_DNSList = $_DNSList.Split(',').Split(' ')
      
      $_ReplaceDNSList= @()
      $_ReplaceDNSList = New-Object System.Net.IPAddress(0x1521A8C0)
      
      #Get the input from the user
      $_ReplaceDNSList= READ-HOST "Enter the Replace DNS Servers IP address(use , comma for addditional IPs )"
      
      #splitting the list of input as array by Comma & Empty Space
      $_ReplaceDNSList = $_ReplaceDNSList.Split(',').Split(' ')
      
      foreach($input in $server)
      {
      
      $LogPath = Get-Item "F:\Scripts\Replace_DNS_IP\PSD Backup\$input.psd1"
      
      $Lines = Get-Content $LogPath | Where-Object {$_ -match "DNSServerAddress"}
      
      Foreach ($Line in $Lines)
      
      {
      
      $IPs = ($Line | Select-String -Pattern $regex -AllMatches).Matches.Value
      
      foreach($IP in $IPs)
      
      {
      
      if(($IP -match $_DNSList[0]) -and ($_ReplaceDNSList[0] -ne $null))
      {
      Write-Host "Replacing $IP DNS Address to $_ReplaceDNSList[0] Address "
      (Get-Content -Path $LogPath).Replace($IP,$_ReplaceDNSList[0]) | Set-Content -Path $LogPath
      }
      elseif(($IP -match $_DNSList[1]) -and ($_ReplaceDNSList[1] -ne $null))
      {
      Write-Host "Replacing $IP DNS Address to $_ReplaceDNSList[1] Address"
      (Get-Content -Path $LogPath).Replace($IP,$_ReplaceDNSList[1]) | Set-Content -Path $LogPath
      }
      elseif(($IP -match $_DNSList[2]) -and ($_ReplaceDNSList[2] -ne $null))
      {
      Write-Host "Replacing $IP DNS Address to $_ReplaceDNSList[2] Address"
      (Get-Content -Path $LogPath).Replace($IP,$_ReplaceDNSList[2]) | Set-Content -Path $LogPath
      }
      elseif(($IP -match $_DNSList[3]) -and ($_ReplaceDNSList[3] -ne $null))
      {
      Write-Host "Replacing $IP DNS Address to $_ReplaceDNSList[3] Address "
      (Get-Content -Path $LogPath).Replace($IP,$_ReplaceDNSList[3]) | Set-Content -Path $LogPath
      }
      else
      {
      Write-Host "DNS IP address is not Matching with provided Input"
      }
      }
      
      }
      }
  • #178272

    Senior Moderator
    Topics: 8
    Replies: 1041
    Points: 3,439
    Helping Hand
    Rank: Community Hero

    @manikandank8286 – please use code formatting tags or use gist.github.com to post code in the forums.

    https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/

  • #178470

    Participant
    Topics: 10
    Replies: 59
    Points: 142
    Helping Hand
    Rank: Participant

    After reviewing your inquiry again, there's a bit of repurposing of variables which can make code harder to follow.

    What is the difference between F:\Scripts\Replace_DNS_IP\server.txt and $LogPath = Get-Item "F:\Scripts\Replace_DNS_IP\PSD Backup\$input.psd1" ? Can you provide samples from both? What is the purpose of both files? .psd1 is normally a PowerShell module manifest file, and not used for logging.

    Are you open to users using an advanced function instead of manually typing IPs with commas? It would take care of the input validation for you.

  • #178512

    Participant
    Topics: 1
    Replies: 3
    Points: 23
    Rank: Member

    Hi Aaron,

    We are using Powershell DSC and SMA servers to build the Windows 2016 servers. All the information(CPU, memory, IP address, storage etc) about the server will be in *.psd1 file. *.psd1 file name will be the server name. i want to update the DNS IP address on more than  50 servers at a time. To avoid the manual work to update the DNS IP address in *.psd1 file, trying to develop a script. Server.txt will have the server name. i have updated the files on the GitHub for your reference https://github.com/Manik8286/Powershellscripts.git.

     

You must be logged in to reply to this topic.