Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Compare Windows Server Host Files

Posted on February 10, 2020 By David Kittell

If you maintain more than one Windows server sometimes you need a quick way to determine the host file differences on each server, the code below will help.

function Read-HostFile() {
	[CmdletBinding()]
	Param(
		[Parameter(Mandatory = $True)]
		[String] $Computer,
		[boolean] $ListComputerName = $true
	)
	$Pattern = '^(?<IP>\d{1,3}(\.\d{1,3}){3})\s+(?<Host>.+)$'
	$File = "\\$Computer\c$\Windows\System32\Drivers\etc\hosts"
	$Entries = @()
	(Get-Content -Path $File) | ForEach-Object {    
		If ($_ -match $Pattern) {   
			if ($ListComputerName) {     
				$Entries += "$Computer,$($Matches.IP),$($Matches.Host)"
			}
			else {
				$Entries += "$($Matches.IP),$($Matches.Host)"
			}
		}
	}
	$Entries
}

function Compare-HostFiles() {
	[CmdletBinding()]
	Param(
		[Parameter(Mandatory = $True)]
		[String] $Computer1,
		[Parameter(Mandatory = $True)]
		[String] $Computer2
	)
	Write-Output "Comparing host file from $($Computer1.ToLower()) to $($Computer2.ToLower())"
	Compare-Object `
		-ReferenceObject $(Read-HostFile `
			-Computer $Computer1 `
			-ListComputerName $false) `
		-DifferenceObject $(Read-HostFile `
			-Computer $Computer2 `
			-ListComputerName $false) `
		-IncludeEqual | 
	Format-Table `
		-AutoSize `
	@{Label = "Host Entry"; Expression = { $_.InputObject } },
	@{Label = "Comparison"; Expression = { $_.SideIndicator } }
}
Clear-Host

# Compare Servers
Compare-HostFiles -Computer1 dev-webserver-p01 -Computer2 dev-webserver-p02

# Read individual files
Read-HostFile 'dev-webserver-p01'
Read-HostFile 'dev-webserver-p02'
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code DNS PowerShell Windows Windows Server

Post navigation

Previous post
Next post

Related Posts

Mac OS X – Show Desktop Image Path

Posted on March 3, 2016March 30, 2016

Ever wonder where the image displayed on your desktop is located? Open Terminal and then run the first command to show the image path, then to hide the path again run the second command. defaults write com.apple.dock desktop-picture-show-debug-text -bool TRUE;killall Dock defaults delete com.apple.dock desktop-picture-show-debug-text;killall Dock Reference: http://osxdaily.com/2011/11/20/show-the-location-path-of-current-wallpaper-in-mac-os-x/ Originally Posted…

Read More

Mac OS X – Terminal – FFMPEG – Convert Music Files FLAC to MP3 or MP4 to MP3

Posted on October 11, 2016January 31, 2019

Prerequisite: Homebrew Essentially you can convert just about anything but this script is specific to FLAC to MP3 <br> brew install ffmpeg –with-vpx –with-vorbis –with-libvorbis –with-vpx –with-vorbis –with-theora –with-libogg –with-libvorbis –with-gpl –with-version3 –with-nonfree –with-postproc –with-libaacplus –with-libass –with-libcelt –with-libfaac –with-libfdk-aac –with-libfreetype –with-libmp3lame –with-libopencore-amrnb –with-libopencore-amrwb –with-libopenjpeg –with-openssl –with-libopus –with-libschroedinger –with-libspeex –with-libtheora –with-libvo-aacenc…

Read More

Azure Red Hat – RHUI Update

Posted on December 27, 2016January 9, 2017

Most of the this script is based on the document “Red Hat Update Infrastructure (RHUI) for on-demand Red Hat Enterprise Linux VMs in Azure” at https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-linux-update-infrastructure-redhat?toc=%2fazure%2fvirtual-machines%2flinux%2ftoc.json My change will first check if the update was already installed, if not it will follow the process to install See updates at: https://gitlab.com/Kittell-RedHat/AzureRepo/raw/master/AzureRedHatRepoUpdate.sh…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes