Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tweak to delete server name

Status
Not open for further replies.

atulram

Systems Engineer
Nov 21, 2019
1
US
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”
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top