GeoIP database update in multiple webserver environment using Windows PowerShell scripting

The MaxMind GeoIP database (shipped with EPiServer Framework) is rather useful when wanting to show geographically specific information to visitors as they surf around the site. Since mapping the correct requests to the proper regions may be rather important generating value, the need of having a fresh database copy quickly comes into focus.

Manually updating the database is of course not at all complicated; it just consists of downloading a file and replacing the old one on the server (as described by David Knipe). However, being reluctant to adding manual steps to an otherwise oh-so-automated deployment process, or using valuable time that could be better spent elsewhere, it is easy to start looking for alternatives. A scheduled EPiServer job quickly comes into mind, and it just happens that Paul Smith wrote one doing just this last summer. Works great, but it showed not to be enough for a system using multiple webservers without file replication possibilities. Running the job multiple times just would not suffice.

As PowerShell scripts seem to allow for automation of almost everything else in this world, writing one for this as well is just natural. It uses the Get-WebFile (v3.6) script by Joel Bennett for downloading the database file and a simple gzip binary for decompression. A general version of the update script can be found at the bottom of this post.

GeoIP database Windows PowerShell update script

The script itself is rather straight forward. This version takes three optional in-parameters with default values; a MaxMind source URL to download from, a target file, as well as an array of paths to update.


   [string]$sourceUrl = "",
   [string]$compressedFile = "GeoLiteCity.dat.gz",
   [string[]]$updateTargets = @("\\server1\c$\path\GeoLiteCity.dat", "\\server2\c$\path\GeoLiteCity.dat")

After using Get-WebFile (36) and gzip (39) to download and decompress the new database, the script backs up the old one. More specifically, it copies the database file from the first server in the $updateTargets array; so if the script is used to deploy the GeoIP database for the first time, or something is wrong with the file, you will be getting exceptions. Suppose it could be done in a more clever way, but as this should not happen, you will definitely know that something is up when it does.

[string[]]$processedTargets = @()
foreach($updateTarget in $updateTargets){
   $processedTargets += $updateTarget
   UpdateFile ".\$decompressedFile" $updateTarget

For rollback purposes, all files that were updated as well as the one attempting to be are added to a $processedTargets string array. This way, if something goes wrong, it is easy to only roll back only the already made changes. The UpdateFile (11) function is used to replace the old database file, and the RollBack (26) one is used for just that. They both solely consist of user feedback echoes and file copy commands.

Running update-geoip.ps1 from PowerShell

It is probably not necessary to supply any input arguments when running the script once it has been configured, as the server environment hopefully will stay the same for some time.

PS D:\SampleCode\Scripts> .\update-geoip.ps1
PS D:\SampleCode\Scripts> .\update-geoip.ps1 "" "foo.txt.gz" (".\foo1.txt",".\foo2.txt")

Automatically updating the GeoIP database using Windows Task Scheduler

If adding to the deployment process feels tiresome, having the Windows scheduler doing the work for you could be a good idea. Making the Task Scheduler run the update-geoip script is not too difficult; I have a previous post about it, just change the Add arguments (optional): entry of the New Action to something in the lines of -Noninteractive -Noprofile -Command “&{C:\path\update-geoip.ps1}”

Source code

Source code:


  1. Paul Smith January 16, 2012

Leave a Reply