| Whew, that’s a mouthful. One of the standard things I do when I install SharePoint is add all of the farm’s web app’s URLs to the HOSTS file of each server, and point them at the server itself, 127.0.0.1. I do this for two reasons. The first is for ease of troubleshooting. If I have a page that’s failing, or some other problem it’s easy to jump on a server and know exactly which log files to look in for information. It also lets me know that that server is working correctly if there are intermittent problems. Second, I like to do this for search crawling purposes. In most cases I point the Index server to itself as opposed to the same machines that end users go to. This way the Indexing process isn’t fighting the end users for resources on the WFEs. It may result in slower crawls, but it also results in happier users. It’s a thing of beauty.
In the past I’ve had to manually edit all those HOSTS files like a punk. It sucked. And of course I’d forget a server or two, or even forget all together. Or I’d remember the initial web apps, but I’d create a couple more later and forget to add them. It was a bad situation, and the source of much swearing. Just when it was darkest, my old pal PowerShell walked through the door and brightened my day.
I decided to write a PowerShell script to do all this for me. It walks through output from Get-SPAlternateURL, pulls out the protocols, the ports and the duplicates, then adds the ones that don’t already exist in the HOSTS file to the HOSTS file. I want to be a good citizen on your servers, so I won’t overwrite any entries in your HOSTS file, and I make a copy of it before I touch anything. You’re welcome. Here’s the text of the script:
#Make backup copy of the Hosts file with today's date
$hostsfile = 'C:\Windows\System32\drivers\etc\hosts'
$date = Get-Date -UFormat "%y%m%d%H%M%S"
$filecopy = $hostsfile + '.' + $date + '.copy'
Copy-Item $hostsfile -Destination $filecopy
# Get a list of the AAMs and weed out the duplicates
$hosts = Get-SPAlternateURL | ForEach-Object {$_.incomingurl.replace("https://","").replace("http://","")} | where-Object { $_.tostring() -notlike "*:*" } | Select-Object -Unique
# Get the contents of the Hosts file
$file = Get-Content $hostsfile
$file = $file | Out-String
# write the AAMs to the hosts file, unless they already exist.
$hosts | ForEach-Object { if ($file.contains($_))
{Write-Host "Entry for $_ already exists. Skipping"} else
{Write-host "Adding entry for $_" ; add-content -path $hostsfile -value "127.0.0.1 `t $_ " }}
# Disable the loopback check, since everything we just did will fail if it's enabled
New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -Value "1" -PropertyType dword
You can download it here. To make sure you get your money’s worth, the script also disables the loopback check for you, at no extra charge. To run this script you’ll have to start up the SharePoint Management Console, and you might have to tweak your PowerShell Execution settings. You’ll have to run this on each server in your farm. If you want to be tricky you can use PowerShell Remoting and run it on all of them at once. Since it won’t overwrite existing HOSTS file entries, it’s safe to run as many times as you want to. If the Loopback check is already disabled that part will report an error. See my Developer Dashboard blog post for my standard disclaimer on downloading PowerShell scripts from the web.
Have fun. Let me know what you think.
tk
ShortURL: http://www.toddklindt.com/edithosts |