PoshCode Archive  Artifact [fe6c6a2d9d]

Artifact fe6c6a2d9dd5b2bd74e4623e80410ccfc6f2a606dc37a03423c957e1c8d5837e:

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

# 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: 2562
# x-archived: 2016-03-18T11:29:26
# x-published: 2011-03-15T10:06: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 information to collection"
                        $temp = "" | Select Computer, IP, Hostname, Notes
                        $new = $_.Split("") | ? {$_ -ne ""}
                        $temp.Computer = $c
                        $temp.IP = $new[0]
                        $temp.HostName = $new[1]
                        If ($new[2] -eq $Null) {
                            $temp.Notes = "NA"
                            }
                        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") {
                    "^\d\w+" {
                        Write-Verbose "Adding information to collection"
                        $temp = "" | Select Computer, IP, Hostname, Notes
                        $new = $_.Split("") | ? {$_ -ne ""}
                        $temp.Computer = $c
                        $temp.IP = $new[0]
                        $temp.HostName = $new[1]
                        If ($new[2] -eq $Null) {
                            $temp.Notes = "NA"
                            }
                        Else {
                            $temp.Notes = $new[2]
                            }
                        $report += $temp
                        }
                    }        
                }#End ElseIf
            Else {
                Write-Verbose "No host file found"
                $temp = "" | Select Computer, IP, Hostname, Notes
                $temp.Computer = $c
                $temp.IP = "NA"
                $temp.Hostname = "NA"
                $temp.Notes = "Unable to locate host file"
                $report += $temp
                }#End Else
            }
        Else {
            Write-Verbose "No computer found"
            $temp = "" | Select Computer, IP, Hostname, Notes
            $temp.Computer = $c
            $temp.IP = "NA"
            $temp.Hostname = "NA"
            $temp.Notes = "Unable to locate Computer"
            $report += $temp            
            }
        }
    }
End {
    Write-Output $report
    }
}