Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > PowerShell script to write SharePoint URLs to your server’s HOSTS file
June 21
PowerShell script to write SharePoint URLs to your server’s HOSTS file

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 

Comments

MOSS 2007 Membership links show deleted sites

This is completely unrelated but just wondering if you have come across this issue where "My SharePoint Sites" list in MOSS 2007 shows links to sites which have been already deleted. I came across this link http://social.technet.microsoft.com/Forums/en-NZ/sharepointgeneral/thread/b00ce250-cdf2-4e3c-876c-8b6571429909 which explains it a little better.

Also have upgraded our farm to April 2011 cumulative update but this has not fixed the problem.
 on 6/26/2011 4:29 PM

Reza Alirezaei

Thanks Todd! Great post!
 on 6/30/2011 9:22 PM

Re: MOSS 2007 Membership links show deleted sites

Sorry, I have not seen that issue.

tk
Todd O. KlindtNo presence information on 7/7/2011 8:26 AM

Re: Reza Alirezaei

Thanks Reza. I appreciate your feedback, especially when it's positive. :)

tk
Todd O. KlindtNo presence information on 7/7/2011 8:27 AM

Adam Preston

As a consultant, I hate having to manually do this too! Great work!

For BackConnectionHostNames, perhaps you could build a string for each URL and use PowerShell to set:

New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa\MSV1_0 -Name “BackConnectionHostNames” -Value “intranet.contoso.com”,”mysites.contoso.com” -PropertyType multistring
 
 on 7/18/2011 6:57 PM

Great script but...

if you have multiple web site, or even just one, but it is tied to a specific IP address, I don't think this will work.
However, you can go in and put the IP addresses into the hosts file, which is still a good idea!
thanks, -bruce
ps. A very real person who met you in Vegas at the last MicroSoft conference, and looking forward to October.
 on 7/26/2011 3:41 PM

Just, a suggestion (DisableLoopbackCheck)

This is an awesome script.  One problem is it will bomb if you already have loopback check turned on.  If you add this code to your script, it will do a check first and only do it if it needs it.

If (-not ((Get-ItemProperty -path "HKLM:\System\CurrentControlSet\Control\Lsa").DisableLoopbackCheck -eq "1"))
{
    New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -value "1" -PropertyType dword -Force
}
 on 8/5/2011 8:42 AM

Recommendation for SP2013

Hi Todd,
What is your recommndation for SP2013 regarding this script?

We utilized this script on a SP2013 install and found that there were some new services (App Fabric) that this didn't play well with.

Thanks!
 on 3/4/2013 2:17 PM

Chinamaya

And this does not work if you have host name site collection...
 on 6/11/2014 9:26 PM

Re: Chinamaya

No, it does not include host named site collection. Obviously it wouldn't be too much work to add that, but I'm afraid that it would bloat the HOSTS file too much.

Any thoughts on that?

tk
Todd O. KlindtNo presence information on 6/11/2014 10:09 PM
1 - 10Next

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person

Twitter


Want a message when I reply to your comment? Put your Twitter handle here.

Attachments

 

 SysKit