by Klaus Graefensteiner
10. August 2011 09:52
Introduction
I needed to parse a version number, a server name and a client name from a file path and use this information to kick off a Continuous Integration build using the TeamCity URL approach I wrote about a few weeks ago. I used PowerShell and Regular Expressions with the –match operator and the regex switch statement.

Figure 1: PowerShell scripting makes hungry. Somebody got creative at a Harry Potter themed birthday party
Here are some example path names:
- \\ShareMan\NUnit\v10.0\V10.0.21789.0\sst20c1\TestResults.xml
- \\ShareMan\NUnit\v10.0\V10.0.21790.0\sst20cw7\TestResults.xml
- \\ShareMan\NUnit\v10.0\V10.0.21817.0\sst20cs2k8\sst20cw7\TestResults.xml
- \\ShareMan\NUnit\v10.0\V10.0.21882.0\sst20cs2k3\sst20csx1\TestResults.xml
The Script
Here is the main script:
$DebugPreference = "Continue"
Set-StrictMode -Version "latest"
function Generate-TestData()
{
$TestResultsFiles = get-childitem -Path "\\ShareMan\NUnit\v10.0\" -Recurse -Filter "TestResults.xml"
foreach($file in $TestResultsFiles)
{
$filepath = $file.FullName
Add-Content -Path C:\TestResultsFilePaths.txt -value $filepath -Encoding UTF8
}
}
function Load-TestData()
{
$TestPaths = Get-Content -Path C:\TestResultsFilePaths.txt -Encoding UTF8
return $TestPaths
}
#\\ShareMan\NUnit\v10.0\V10.0.21789.0\sst20c1\TestResults.xml
#\\ShareMan\NUnit\v10.0\V10.0.21790.0\sst20cw7\TestResults.xml
#\\ShareMan\NUnit\v10.0\V10.0.21817.0\sst20cs2k8\sst20cw7\TestResults.xml
#\\ShareMan\NUnit\v10.0\V10.0.21882.0\sst20cs2k3\sst20csx1\TestResults.xml
$TestData = Load-TestData
function Get-TestConfiguration([string] $path)
{
$Configuration = @{}
if($path -match "^\\\\ShareMan\\NUnit\\v10\.0\\V(?<BUILD>(10\.0\.\d{5}\.0))\\((?<CLIENT>(sst20c1))|(?<CLIENT>(sst20cw7))|(?<SERVER>(.+?))\\(?<CLIENT>(.+?)))\\TestResults\.xml$")
{
$Configuration["BUILD"] = $matches.BUILD
$client = $matches.CLIENT
$Configuration["CLIENT"] = $client
Write-Debug $client
switch -regex ($client)
{
"sst20c1"
{
$Configuration["SERVER"] = $client
}
"sst20cw7"
{
if($Configuration["SERVER"] -eq $null)
{
$Configuration["SERVER"] = "sst20cs2k8"
}
else
{
$Configuration["SERVER"] = $matches.SERVER
}
}
default
{
$Configuration["CLIENT"] = $matches.CLIENT
$Configuration["SERVER"] = $matches.SERVER
}
}
return $Configuration
}
else
{
throw "$path doesn't contain sufficient test configuration data"
}
}
function Lookup-BuildTypeID([string] $TablePath, [string] $Server, [string] $Client)
{
. $TablePath
$LookupKey = $Server.ToLower() + "\" + $Client.ToLower()
Write-Debug $LookupKey
return $LookupTable[$LookupKey]
}
function Copy-TestArtifacts([string] $Source, [string] $Destination)
{
Copy-Item
}
foreach($path in $TestData)
{
Write-Debug $path
try
{
$Configuration = Get-TestConfiguration -path $path
}
catch
{
$_
}
Lookup-BuildTypeID -TablePath "LookupTable.ps1" -Server $Configuration.SERVER -Client $Configuration.CLIENT
}
The lookup table script that maps server/client combinations to a TeamCity build type id is listed here:
$LookupTable = @{}
$LookupTable["sst20c1\sst20c1"] = "bt3"
$LookupTable["sst20cs2k8\sst20cw7"] = "bt4"
$LookupTable["sst20cs2k3\sst20csx1"] = "bt5"
Download
The script and some sample data can be downloaded here: ParseTestResultsFilePaths.zip
Ausblick
I like it!