PowerShell Script to Add Entries to the Hosts File

I prefer to develop locally on my own system before moving my code to a live staging or production environment (and I suggest you do too!).  On my system, I typically have several playgrounds where I am working on different projects, testing plugins, or playing with the latest beta of WordPress. To simplify access to all of these areas, I setup Virtual Hosts in XAMPP (another blog post in itself) and add names to my hosts file, to provide easy to remember URLs to each local site.

For those of you not familiar with the hosts file:

The hosts file is a computer file used by an operating system to map hostnames to IP addresses. The hosts file is a plain text file, and is conventionally named hosts.

Wikipedia

Today, the hosts file is seldom used, as the Domain Name System (DNS) handles pretty much all of the domain name (i.e. philerb.com) to IP address (i.e. 198.74.61.183) mapping for us. But, all operating systems still have this file and it makes it pretty handy to make our own self-use names.

I tend to work between several computers and synchronize my XAMPP installation to each of them.  So I can easily update the hosts file on each system, I store a PowerShell script in my XAMPP directory which handles this for me.

You will need to be an administrator on the computer to run this. If you are using User Account Control (default in Windows Vista and above), then you will need to use “Run as Administrator” to start PowerShell, otherwise you won’t be able to touch the hosts file.

$hostsEntriesToAdd = @(
    ("127.0.0.1","wpbeta.localhost"),
    ("127.0.0.1","client1.localhost"),
    ("127.0.0.1","client2.localhost")
)

$hostsFile = "C:\Windows\System32\drivers\etc\hosts"

$needNewline = $true
$openHostsFileWhenDone = $false

ForEach ( $hostsEntryToAdd in $hostsEntriesToAdd ) {
    $testResult = Select-String -Pattern $hostsEntryToAdd[1] $hostsFile

    If ( $testResult -ne $NULL ) {
        If ( $testResult | Select-String -Pattern $hostsEntryToAdd[0] ) {
            Write-Output "[INFO] The entry already exists for $($hostsEntryToAdd[1])"
        }
        Else {
            Write-Output "[ERROR] A conflicting entry exists for $($hostsEntryToAdd[1]):"
            Write-Output "$($testResult)"
            $openHostsFileWhenDone = $true
        }
    }
    Else {
        Write-Output "[INFO] Creating entry for $($hostsEntryToAdd[1])"
        If ( $needNewLine -eq $true ) {
            #Add a blank line to the file
            Add-Content $hostsFile ""
            $needNewline = $false
        }
        Add-Content $hostsFile "$($hostsEntryToAdd[0])    $($hostsEntryToAdd[1])"
    }
}

If ( $openHostsFileWhenDone -eq $true ) {
    Write-Output ""
    Write-Output "Since there was a conflict (see above), let's open the hosts file and have a look."
    Read-Host "Press ENTER to continue"
    notepad.exe $hostsFile
}

The script will tell you what it’s doing – if it needs to add something or not and if there’s a conflict. It will even open the hosts file in Notepad, if it detects a conflict that you need to fix.

Updates to “Sending mail with PowerShell”

By far, my most popular blog post to date is Sending mail with PowerShell – in the past month alone, it has nearly a thousand unique page views.

I’ve had several questions on the post and today I made some significant updates to it to reflect various different scenarios, including multiple recipients and attachments (and multiple attachments).

If there’s a scenario I’ve missed or you have any questions, add a comment to that post.

Google Analytics on WordPress Multisite

I recently had a need to add Google Analytics code to every site in a WordPress sub-folder multisite network.

Obviously, there are a lot of plugins for adding GA code to a WordPress site.  Most of these are intended for single sites – some will work on multisite, but they expect you to enter the tracking code on each site in the network.  In some cases this may be nice, as it allows you have different tracking codes for different sites, but it also allows an admin for each site to change the code.  That, coupled with the extra work of manually adding the code to each site led me down a different path.

Continue reading

NEPA BlogCon 2013: WordPress Customization & Security

Back in October, I spoke at NEPA BlogCon with Joe Casabona about WordPress Customization and Security.

This was my first time at BlogCon and I have to say that The Fearsome Foursome put on an excellent event!  It was great to see this community of bloggers and IT pros get together in Northeast PA.

Here is the video from our session:

Videos of all of the sessions are available on the NEPA BlogCon YouTube page.

Limits & Ethics

As an enterprise e-mail administrator, I’m regularly asked about the size of attachments that our users can send.  I often give the answer as “that depends” (more on this below).

Less frequently, after giving my answer, I’m asked “can that be changed?”  And much less frequently, though still several times in my career “alright, that’s officially the limit, but can it be changed for me?”

Continue reading