Tuesday, January 13, 2009

Switching Website Root With PowerShell

Gasp! Jim is actually writing a technical blog post!

I'm using Selenium to write UI/feature tests for Community Server and Evolution. I use these tests both as feature acceptance and as regression tests to make sure we didn't bust something while working on other parts of the system. In order for me to regression test, I need to easily switch my website between the release and latest trunk.

For example, I’ve got two virtual directories in IIS, one for release, one for functional tests against trunk:

IIS

   Default Web   (http://localhost)

           Community Server Release  (http://localhost/CS)

                  Filesystem: C:\Program Files\Telligent\Community Server 2008.5\Web

           CS from Subversion’s trunk (http://localhost/csfunctionaltests)

                  Filesystem: D:\TelligentSvn\CommunityServer\Core\Trunk\CommunityServer.Web

I can change this manually in IIS Manager via the virtual directory's property sheet, but that's slow and a lot of mouse clicks. I hate the mouse. Keyboard, FTW!

PowerShell, to the rescue! I'm only just (finally!) getting back to PowerShell after a couple years' hiatus, so the following script likely isn't as cool as it could be. Still, it does the job and I'll refactor later if needed.

param ([switch]$showCurrent, [switch]$trunk, [switch]$release)

if ($trunk -and $release)

{

"Only one of -trunk or -release can be used."

exit

}

$iis=[ADSI]"IIS://localhost/W3SVC/1/Root"

$cstests = $iis.psbase.children | where {$_.AppRoot -eq '/LM/W3SVC/1/Root/csfunctionaltests'}

if ($showCurrent)

{

     "`nCurrent path: " + $cstests.Path + "`n"

     exit

}

if ($trunk)

{

     # Path for trunk

     $path = "D:\TelligentSvn\CommunityServer\Core\Trunk\CommunityServer.Web"

}

if ($release)

{

     # Path for release

     $path = "C:\Program Files\Telligent\Community Server 2008.5\Web"

}

$cstests.Put("Path",$path)

$cstests.SetInfo()

iisreset /noforce

$cstests = $iis.psbase.children | where {$_.AppRoot -eq '/LM/W3SVC/1/Root/csfunctionaltests'}

"`nNew path for csfunctionaltests: " + $cstests.Path + "`n"

Save this as ChangeRoot.ps1, edit the trunk and release paths for your environment and invoke it with "ChangeRoot.ps1 -trunk" or "ChangeRoot.ps1 -release" to change appropriately. Use the "-showCurrent" to tell you which path you're currently using.

No comments:

Subscribe (RSS)

The Leadership Journey