Parsing path names with PowerShell and Regular Expressions

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.

photo

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!

Tags: , , , ,

PowerShell | Tips & Tricks | How To | Continuous Integration

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2013, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2013 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/21/2013 9:29:11 AM (PST Pacific Standard Time UTC DST -7)