none
Tweak to delete server name RRS feed

  • Question

  • 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 ?

    If some one can provide hints then i can modify the script accordingly.

    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”

    Monday, November 18, 2019 11:22 AM

Answers

All replies

  • Hi AtulRam

    For a mass treatment use a foreach loop like  the following :

    $Path =C:\pathto\exportFile.txt

    $Servers = Get-Content -Path $Path

    Foreach ($Server in $Servers)

    {

    Do something to $Server

    }

    Regards

    Olivier

    Monday, November 18, 2019 12:30 PM
  • Hi Olivier,

    thanks for replying.

    can we also get an output like success/failure ?

    Thanks.

    Tuesday, November 19, 2019 4:39 AM
  • I added the condition with for loop "Remove-Item -Recurse $Server\$Path"  but i gives me error.

    I think i am missing something with the input.

    can you please suggest. 

    Tuesday, November 19, 2019 8:42 AM
  • Hi AtulRam

    Of course it's possible by using Try... catch, .... finally

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-5.1

    i.e. :

    try

    {

    Do Something

    Write-Host "SUCCESS"

    } # If a error occurs, then exec Catch section

    catch

    { Write-Host "FAILED"

    Write-Host "An error occurred: $_.Exception.Message"

    Do something else

    } # If an error occurs, this section runs

    Finally

    {

    Do something

    Write-Host "The script is done"

    } # this section (optional) runs in every case.

    Regards

    Olivier




    • Edited by Oliv - TheFrog Tuesday, November 19, 2019 8:50 AM
    • Marked as answer by AtulRam Thursday, November 21, 2019 3:57 PM
    Tuesday, November 19, 2019 8:49 AM
  • thanks a lot.

    I will need more time to integrate your suggestions into the script (noob :D )

    Thursday, November 21, 2019 1:54 PM