0

I am looking to grab the file host info from 5 machines Blue,Green,Yellow,White,Red all names stored in a file C:\servers.csv. How can I get the output saved somewhere else? So far this is what I have come up with:

$servernames = @(import-csv "C:\servers.csv")

foreach ( $servername in $servernames ) {
    Get-Content -Path "$env:windir\system32\drivers\etc\hosts" | Set-content C:\result.txt
}

I also tried this:

$Computers = @("Red","Yellow","Blue","Green","White")

$File = "$env:windir\system32\drivers\etc\hosts"

foreach ($s in $Computers) { 
    Get-Content $File -Exclude localhost | set-content C:\result.txt
}

Any help would be appreciated.

1
  • 1
    It's not clear what you're attempting to do, the $file var is always the same (your local computer) on each iteration. If those are remote computers you would need to use the UNC path of each computer or use Invoke-Command. Commented Jun 16, 2021 at 3:56

2 Answers 2

3

If i'm understanding you corretcly, you just want to grab the content from within each host file pertaining to each computer, then save it else where? You can take an approach like so:

$Computers = @("Red","Yellow","Blue","Green","White")


foreach ($s in $Computers) {

    $Host_Content = Invoke-Command -ComputerName  $s -ScriptBlock {
        Get-Content -Path  "$env:windir\system32\drivers\etc\hosts"  | 
            ForEach-Object -Process { 
                $_ | Where-Object -FilterScript { $_ -notmatch "localhost" -and $_ -match "((?:(?:0?0?\d|0?[1-9]\d|1\d\d|2[0-5][0-5]|2[0-4]\d)\.){3}(?:0?0?\d|0?[1-9]\d|1\d\d|2[0-5][0-5]|2[0-4]\d))" }
            }
    }

    $Host_Content 
    $Host_Content | Out-File -FilePath "C:\temp\$S.txt" -Force

}

This will allow you to save the host files content from each computer into it's own text file with the corresponing computer it got it from.

It's just a matter of what Santiago pointed out. Either provide the UNC path, or use Invoke-Command to send the command over to the remote PC, do the work, and return it. I assumed you didn't want the localhost entries from the files so I excluded it in the file content itself. You can also use Get-DNSClientCache for cached dns entries.

EDIT:

Added some regex matching I found on a google search to narrow it down to lines with just IPs

Sign up to request clarification or add additional context in comments.

2 Comments

Worth mentioning, -ComputerName can also use the $computers array for faster execution.
Ahhh good point! Completely forget about that most of the time
-1

Are you trying to get the info from a unc path?

$Computers = @("Red","Yellow","Blue","Green","White")

foreach ($computer in $Computers) { 
    Get-Content "\\$computer\c`$\windows\system32\drivers\etc\hosts" -Exclude localhost | set-content C:\result.txt
}

1 Comment

The -Exclude parameter for Get-Content doesn't do what you suggest it does here. It is a filter for Path, not for the contents of the items it finds. From the documentation Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.