# ============================================================================================== # # Microsoft PowerShell Source File -- Created with SAPIEN Technologies PrimalScript 2009 # # NAME: Parse-Hosts.ps1 # v1.1 # USAGE: .\parse-hosts.ps1 [[-computername] computername ] [-progress] # # AUTHOR: Jeffery Hicks, PowerShell MVP , SAPIEN Technologies, Inc. # DATE : 2/27/2009 # # COMMENT: This script will connect to a computer's $ADMIN share to parse the HOSTS # file. Assuming entries are found, a custom object is written to the pipeline with the # IP Address, name and comment, if any. # # Specifying -progress will use Write-Progress to display what the script is doing. # For single machines you probably won't see anything but it can be useful if you are # running through a list of computers. # # The script will first attempt to ping the computer and only continue if a ping succeeds. # # DISCLAIMER AND WARNING: # THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY # KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. # TEST THOROUGHLY IN A NON-PRODUCTION ENVIRONMENT. IF YOU DON'T KNOW WHAT THIS # SCRIPT WILL DO...DO NOT RUN IT! # # ============================================================================================== Param([string]$computername=$env:computername, [switch]$progress) function TestPing([string]$address) { $wmi = Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = '$address'" if ($wmi.statuscode -eq 0) { $true } else { $false } } if ($progress) { $activity="Analyzing Hosts file" $status=("...on {0}" -f $computername.ToUpper()) Write-Progress -Activity $activity -status $status } if (TestPing $computername) { #only proceed if computer can be pinged $Hosts= "\\$computername\admin$\System32\drivers\etc\hosts" if ((Get-Item $Hosts -ea "SilentlyContinue").Exists) { #if hosts file is found, then parse it. if ($progress) { $current="Parsing $hosts" Write-Progress -Activity $activity -status $status -current $current } #define a regex to return first NON-whitespace character [regex]$r="\S" #strip out any lines beginning with # and blank lines $HostsData = Get-Content $Hosts | where { (($r.Match($_)).value -ne "#") -and ($_ -notmatch "^\s+$") -and ($_.Length -gt 0) } if ($HostsData){ #only process if something was found in HOSTS $HostsData | foreach { #created named values $_ -match "(?\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(?\S+)" | Out-Null $ip=$matches.ip $hostname=$matches.hostname #get a comment if defined for the line if ($_.contains("#")) { $comment=$_.substring($_.indexof("#")+1) } else { $comment=$null } #create a blank object $obj = New-Object PSObject #the computername property could be the name of the computer whose hosts file #you are enumerating $obj | Add-Member Noteproperty -name "Computername" -Value $computername.ToUpper() $obj | Add-Member Noteproperty -name "IP" -Value $ip $obj | Add-Member Noteproperty -name "Hostname" -Value $hostname $obj | Add-Member Noteproperty -name "Comment" -Value $comment write $obj } #end ForEach } #end If $HostsData else { Write-Host ("{0} has no entries in its HOSTS file." -f $computername.toUpper()) -foreground Magenta } } #end If Hosts exists else { Write-Warning "Failed to find $hosts" } } #end If TestPing else { Write-Warning ("Failed to connect to {0}" -f $computername.ToUpper()) } if ($progress) { Write-Progress -Activity $activity -status $status -completed } #end of script