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.
$filevar 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 useInvoke-Command.