Tweak PS script to delete server name

Welcome Forums General PowerShell Q&A Tweak PS script to delete server name

Viewing 1 reply thread
  • Author
    Posts
    • #190306
      Participant
      Topics: 1
      Replies: 0
      Points: 14
      Rank: Member

      Hello,

      I have below script to input server host name or IP which returns the text files for those host names/IP as output.

      Then i have to manually delete those host names from the text files.

      How can I tweak the script to directly delete that host name from text file and save it ? Also instead of one host name as input can we put down a list of servers to search and delete with success/failure log ?

      If someone can suggest how to achieve this.

      Thanks .

      [string]$Server = Read-Host -Prompt 'Input the server name to search and hit Enter'
      #[string]$Path1 = \\servername.com\d$
      [string]$Path = "\\servername.com\d$"

      #Do {

      write-host $Server
      write-host "Searching for the Server $Server in $Path .....search is in progress.. `n `n" -foregroundcolor green

      $PathArray = @()

      $Time = (Measure-Command {

      Get-ChildItem $Path -recurse -Filter "*.txt" | Where-Object {$_.Name -notmatch "backup","Notes","test","copy"} |
      Where-Object { $_.Attributes -ne "Directory"} |
      ForEach-Object {
      If (Get-Content $_.FullName | Select-String -Pattern $Server) {
      $PathArray += $_.FullName

      }
      }
      }).totalminutes

      Write-Host "$Server is located in the following paths : " -foregroundcolor magenta
      $PathArray | ForEach-Object {$_}
      if (!$PathArray) { Write-Host "Server does not exist in the selected path. Please search in other locations. `n" -foregroundcolor Red }
      #write-host $PathArray
      #write-host "`n Time taken to search is : $time minutes. " -foregroundcolor green

      #Read-Host -Prompt “Press Enter to exit”

    • #190339
      Participant
      Topics: 10
      Replies: 1284
      Points: 1,084
      Helping Hand
      Rank: Community Hero

      Please use the pre tags to format your code. I'd recommend breaking things up a bit, collect the files, loop thru the files, loop thru the lines in the file, if a match is found overwrite the file.

      $server = 'Server123'
      $path = 'C:\Scripts'
      $files = Get-ChildItem $Path -recurse -Filter "*.txt" -File | 
               Where-Object {$_.Name -notmatch "backup","Notes","test","copy"}
      
      
      foreach ( $file in $files ) {
      
          $content = Get-Content $file.FullName
          $match = $false
      
      
          $newContent = foreach ( $row in $content ) {
              If ($row | Select-String -Pattern $Server) {
                  Write-Host ('Match found for {0} in file {1}' -f $server, $file.FullName)
                  $match = $true
              }
              else {
                  $row
              }
          }
      
      
          if ( $match ) {
              Write-Host ('Before: {0}{0}{1}' -f [environment]::NewLine,($content -join [environment]::NewLine))
              
              Write-Host ('{0}{0}After: {0}{0}{1}' -f [environment]::NewLine,($newContent -join [environment]::NewLine))
      
              #$newContent | Out-File -FilePath $file.FullName -Force
          }
      
      }
      

      Then I can check the files are filtering:

      PS C:\Users\rasim> $files
      
      
          Directory: C:\Scripts
      
      
      Mode                LastWriteTime         Length Name                                                                                                                                                                                                            
      ----                -------------         ------ ----                                                                                                                                                                                                            
      -a----        2/22/2019   9:54 AM            258 file1.txt                                                                                                                                                                                                       
      -a----        2/22/2019   9:55 AM              0 file2.txt                                                                                                                                                                                                       
      -a----       11/25/2019   9:28 AM            100 Temp0.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp1.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp10.txt                                                                                                                                                                                                      
      -a----       11/22/2019   4:25 PM              0 Temp2.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp3.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp4.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp5.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp6.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp7.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp8.txt                                                                                                                                                                                                       
      -a----       11/22/2019   4:25 PM              0 Temp9.txt                                                                                                                                                                                                       
      

      and then I can test the match and what my newContent would be:

      Match found for Server123 in file C:\Scripts\Temp0.txt
      Before: 
      
      Server1
      Server2
      Server3
      Server123
      Server153
      
      
      After: 
      
      Server1
      Server2
      Server3
      Server153
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.