Private/Hosts/Get-HostsEntry.ps1
<# .SYNOPSIS Retrieves an entry from the HOSTS file. .PARAMETER Hostname The hostname to retrieve. .PARAMETER Hosts The path to the hosts file. #> Function Get-HostsEntry { [CmdletBinding()] Param( [Parameter(Position = 1, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$Hostname , [Parameter(Position = 2)] [ValidateScript({ Test-Path $_ -PathType "Leaf" })] [string]$Hosts = (Join-Path $env:windir -ChildPath "System32\drivers\etc\hosts") ) Begin { $escapedHostname = [regex]::Escape($Hostname) } Process { $match = Get-Content $Hosts | Where-Object { $_ -notmatch "^\s*#" } | Select-String "\b$($EscapedHostname)\b" | Select-Object -First 1 If ($match -match "^\s*([\d\.]+|[0-9a-f\:]+)\b") { $Matches[1] } } } |