PoshCode Archive  Artifact [224430b728]

Artifact 224430b72832b5b21a2db266819e8332185239f795c8fb363cc0e42fc9820ee1:

  • File Get-HostsFile.ps1 — part of check-in [a70951df0d] at 2018-06-10 13:12:26 on branch trunk — This function will allow you to retreive the contents of a hosts file on a local or remote system. This can be used with one or more systems. (user: Boe Prox size: 6075)

# encoding: ascii
# api: powershell
# title: Get-HostsFile
# description: This function will allow you to retreive the contents of a hosts file on a local or remote system. This can be used with one or more systems.
# version: 1.1
# type: function
# author: Boe Prox
# license: CC0
# function: Get-HostsFile
# x-poshcode-id: 2568
# x-archived: 2016-03-18T11:22:44
# x-published: 2011-03-17T14:29:00
#
#
Function Get-HostsFile {
<#
.SYNOPSIS
    Retrieves the contents of a hosts file on a specified system.
.DESCRIPTION
    Retrieves the contents of a hosts file on a specified system.
.PARAMETER ComputerName
    The computers to access.
.NOTES
    Name: Get-HostsFile
    Author: Boe Prox
    DateCreated: 15Mar2011

    1.1 - 2011-03-17 - Jason Archer
        Improved pipeline support (and fixed positional usage).
        Added custom object creation and incremental output (better performance and cleaner code).
        For local host, use local path.
        Added error messages for error conditions.
    1.0 - 2011-03-15 - Boe Prox
        Initial release.
.LINK
    http://boeprox.wordpress.com
.EXAMPLE
    Get-HostsFile "server1"

    Description
    -----------    
    Retrieves the contents of the hosts file on 'server1'.
#>

[CmdletBinding()]
Param(
    [Parameter(Position = 0, ValueFromPipeline = $True, ValueFromPipelineByPropertyName = $True)]
    [ValidateNotNull()]
    [string[]]$ComputerName = "localhost"
)

Begin {
    $PSBoundParameters.GetEnumerator() | Foreach-Object {  
        Write-Verbose "Parameter: $_" 
    }
}

Process {
    Write-Verbose "Starting process of computers"
    ForEach ($c in $ComputerName ) {
        Write-Verbose "Testing connection of $c"
        If (Test-Connection -ComputerName $c -Quiet -Count 1) {
            Write-Verbose "Validating path to hosts file"

            if ($c -eq "localhost") {
                $root = "C:"
            } else {
                $root = "\\$c\C`$"
            }

            If (Test-Path "$root\Windows\system32\drivers\etc\hosts") {
                Switch -regex -file ("$root\Windows\system32\drivers\etc\hosts") {
                    "^#\w+" {
                    }
                    "^\d\w+" {
                        Write-Verbose "Adding IPV4 information to collection"

                        $new = $_.Split("") | Where-Object {$_ -ne ""}
                        If ($new[2] -eq $null) {
                            $notes = $null
                        } Else {
                            $notes = $new[2]
                        }
                        New-Object PSObject -Property @{
                            ComputerName = $c
                            IPV4 = $new[0]
                            IPV6 = $null
                            Hostname = $new[1]
                            Notes = $notes
                        }
                    }
                    Default {
                        If (!("\s+" -match $_ -OR $_.StartsWith("#"))) {
                            Write-Verbose "Adding IPV6 information to collection"

                            $new = $_.Split("") | ? {$_ -ne ""}
                            If ($new[2] -eq $null) {
                                $notes = $null
                            } Else {
                                $notes = $new[2]
                            }
                            New-Object PSObject -Property @{
                                ComputerName = $c
                                IPV4 = $null
                                IPV6 = $new[0]
                                Hostname = $new[1]
                                Notes = $notes
                            }
                        }
                    }                        
                }
            } ElseIf (Test-Path "$root\WinNT\system32\drivers\etc\hosts") {
                Switch -regex -file ("$root\WinNT\system32\drivers\etc\hosts") {
                    "^#\w+" {
                    }
                    "^\d\w+" {
                        Write-Verbose "Adding IPV4 information to collection"

                        $new = $_.Split("") | ? {$_ -ne ""}
                        If ($new[2] -eq $null) {
                            $notes = $null
                        } Else {
                            $notes = $new[2]
                        }
                        New-Object PSObject -Property @{
                            ComputerName = $c
                            IPV4 = $new[0]
                            IPV6 = $null
                            Hostname = $new[1]
                            Notes = $notes
                        }
                    }
                    Default {
                        If (!("\s+" -match $_ -OR $_.StartsWith("#"))) {
                            Write-Verbose "Adding IPV6 information to collection"

                            $new = $_.Split("") | ? {$_ -ne ""}
                            If ($new[2] -eq $null) {
                                $notes = $null
                            } Else {
                                $notes = $new[2]
                            }
                            New-Object PSObject -Property @{
                                ComputerName = $c
                                IPV4 = $null
                                IPV6 = $new[0]
                                Hostname = $new[1]
                                Notes = $notes
                            }
                        }
                    }                        
                }        
            } Else {
                ## TODO: Could use the properly localized path not found error
                Write-Error "Unable to locate host file on computer: $c"
            }
        } Else {
            ## TODO: Could use the properly localized can not locate host error
            Write-Error "Unable to locate computer: $c"    
        }
    }
}
}