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”
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”