PoshCode Archive  Artifact [749cb76bbd]

Artifact 749cb76bbd5809b01f3ee5b1528a23385fd6b3843a4e4c11d3dad601fd98ae72:

  • File Get-HostsFile.ps1 — part of check-in [ea054b98d4] at 2018-06-10 13:12:23 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: 6407)

# 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: 0.1
# type: function
# author: Boe Prox
# license: CC0
# function: Get-HostsFile
# x-poshcode-id: 2565
# x-derived-from-id: 2568
# x-archived: 2016-03-05T10:54:46
# x-published: 2012-03-15T15:17: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 Computer
    Computer name to view host file from
.NOTES  
    Name: Get-HostsFile
    Author: Boe Prox
    DateCreated: 15Mar2011 
.LINK 
    http://boeprox.wordpress.com       
.EXAMPLE  
    Get-HostsFile "server1"

Description
-----------    
Retrieves the contents of the hosts file on 'server1'


#> 
[cmdletbinding(
	DefaultParameterSetName = 'Default',
	ConfirmImpact = 'low'
)]
    Param(
        [Parameter(
            ValueFromPipeline = $True)]
            [string[]]$Computer                                               
                        
        )
Begin {
    $psBoundParameters.GetEnumerator() | % {  
        Write-Verbose "Parameter: $_" 
        }
        If (!$PSBoundParameters['computer']) {
        Write-Verbose "No computer name given, using local computername"
        [string[]]$computer = $Env:Computername
        }
    $report = @()
    }
Process {
    Write-Verbose "Starting process of computers"
    ForEach ($c in $computer) {
        Write-Verbose "Testing connection of $c"
        If (Test-Connection -ComputerName $c -Quiet -Count 1) {
            Write-Verbose "Validating path to hosts file"
            If (Test-Path "\\$c\C$\Windows\system32\drivers\etc\hosts") {
                Switch -regex -file ("\\$c\c$\Windows\system32\drivers\etc\hosts") {
                    "^\d\w+" {
                        Write-Verbose "Adding IPV4 information to collection"
                        $temp = "" | Select Computer, IPV4, IPV6, Hostname, Notes
                        $new = $_.Split("") | ? {$_ -ne ""}
                        $temp.Computer = $c
                        $temp.IPV4 = $new[0]
                        $temp.HostName = $new[1]
                        If ($new[2] -eq $Null) {
                            $temp.Notes = $Null
                            }
                        Else {
                            $temp.Notes = $new[2]
                            }
                        $report += $temp
                        }
                    Default {
                        If (!("\s+" -match $_ -OR $_.StartsWith("#"))) {
                            Write-Verbose "Adding IPV6 information to collection"
                            $temp = "" | Select Computer, IPV4, IPV6, Hostname, Notes
                            $new = $_.Split("") | ? {$_ -ne ""}
                            $temp.Computer = $c
                            $temp.IPV6 = $new[0]
                            $temp.HostName = $new[1]
                            If ($new[2] -eq $Null) {
                                $temp.Notes = $Null
                                }
                            Else {
                                $temp.Notes = $new[2]
                                }
                            $report += $temp
                            }
                        }                        
                    }
                }#EndIF
            ElseIf (Test-Path "\\$c\C$\WinNT\system32\drivers\etc\hosts") {
                Switch -regex -file ("\\$c\c$\WinNT\system32\drivers\etc\hosts") {
                    "^#\w+" {
                        }
                    "^\d\w+" {
                        Write-Verbose "Adding IPV4 information to collection"
                        $temp = "" | Select Computer, IPV4,IPV6, Hostname, Notes
                        $new = $_.Split("") | ? {$_ -ne ""}
                        $temp.Computer = $c
                        $temp.IPV4 = $new[0]
                        $temp.HostName = $new[1]
                        If ($new[2] -eq $Null) {
                            $temp.Notes = $Null
                            }
                        Else {
                            $temp.Notes = $new[2]
                            }
                        $report += $temp
                        }
                    Default {
                        If (!("\s+" -match $_ -OR $_.StartsWith("#"))) {
                            Write-Verbose "Adding IPV6 information to collection"
                            $temp = "" | Select Computer, IPV4, IPV6, Hostname, Notes
                            $new = $_.Split("") | ? {$_ -ne ""}
                            $temp.Computer = $c
                            $temp.IPV6 = $new[0]
                            $temp.HostName = $new[1]
                            If ($new[2] -eq $Null) {
                                $temp.Notes = $Null
                                }
                            Else {
                                $temp.Notes = $new[2]
                                }
                            $report += $temp
                            }
                        }                        
                    }        
                }#End ElseIf
            Else {
                Write-Verbose "No host file found"
                $temp = "" | Select Computer, IPV4, IPV6, Hostname, Notes
                $temp.Computer = $c
                $temp.IPV4 = "NA"
                $temp.IPV6 = "NA"                
                $temp.Hostname = "NA"
                $temp.Notes = "Unable to locate host file"
                $report += $temp
                }#End Else
            }
        Else {
            Write-Verbose "No computer found"
            $temp = "" | Select Computer, IPV4, IPV6, Hostname, Notes
            $temp.Computer = $c
            $temp.IPV4 = "NA"
            $temp.IPV6 = "NA"            
            $temp.Hostname = "NA"
            $temp.Notes = "Unable to locate Computer"
            $report += $temp            
            }
        }
    }
End {
    Write-Output $report
    }
}