Get host file entries
Posted in Powershell
Hello,
This is a quick script to help you in your daily work, in fact a script to list entries of you hosts file 🙂
Function Get-HostFileEntry { $HostOutput = @() $HostFile = $env:windir + "\System32\drivers\etc\hosts" [regex]$r="\S" Get-Content $HostFile | ? { (($r.Match($_)).value -ne "#") -and ($_ -notmatch "^\s+$") -and ($_.Length -gt 0) } | % { $_ -match "(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?<HOSTNAME>\S+)" | Out-Null $HostOutput += New-Object -TypeName PSCustomObject -Property @{'IP'=$matches.ip;'Hostname'=$matches.hostname} } $HostOutput }
Regards,
Somehow I bumped into this blog post.
I recently wrote an article that goes a bit further in the Hosts file management with powershell.
I wrote a complete class that manages local and remote hosts files based on a powershell class. In the article I go through the complete think process that is needed to design and write a class. http://powershelldistrict.com/managing-hosts-file-using-powershell-classes/
Let me know what you think of it 🙂
Stéphane