Skip Navigation LinksHome > View Post

Exporting VSTS load test results to a new database with PowerShell

He's been threatening it for a while now, but at long last, my good friend and colleague Mr Colin Beales has finally started blogging. Check out his introductory post: This is me....

This is good news for fans of Visual Studio Team System so if you have any sense you'll get over there and pound on his blog and subscribe to his feed to make sure he gets some traffic and is encouraged to keep posting. What are you waiting for?

His first real post Exporting VSTS Load Test results to a new database is well worth a read for anyone who uses the load test functionality available in Visual Studio and he even includes some code.

What's this got to do with PowerShell?

Keen readers of theJoyOfCode.com will have seen that I'm having a bit of fun getting friendly with PowerShell of late and Colin has kindly provided me with the inspiration I needed to demonstrate the use of powershell for something a little bit more complicated than my previous, and admittedely very simple examples.

The code sample colin provides basically traverses a directory looking for .trx files (created by VSTS load test) and swaps the connection string value with a the encrypted version taken from the local registry. Fair game for a tiny powershell script... and here it is:

$dir = "c:\temp"
$key = get-itemproperty HKCU:Software\Microsoft\VisualStudio\8.0\EnterpriseTools\QualityTools\Controller
$connectionstring = $key.LoadTestResultsConnectString
$re = [regex] "(<m_resultsRepositoryConnectString.*?>).*?(</m_resultsRepositoryConnectString>)"

dir $dir *.trx -recurse | foreach {
$in = [System.IO.File]::ReadAllText($_.FullName)
$out = $re.Replace($in, "`$1" + $connectionstring + "`$2")
[System.IO.File]::WriteAllText($_.FullName, $out)
}

And that's it. You can just C+P this whole thing into PowerShell and you're away. Let's take a look at it line-by-line to see how it works:

line by line

First we just setup the name of the directory we want to scan and store it in a variable called $dir

$dir = "c:\temp"

Next we get the value of the connection string out of the registry and store it in a variable called $connectionstring for use later

$key = get-itemproperty HKCU:Software\Microsoft\VisualStudio\8.0\EnterpriseTools\QualityTools\Controller
$connectionstring = $key.LoadTestResultsConnectString

Now we create a regular expression that will scan the .trx files and find the appropriate area to replace. Note that our regex differs from Colin's slightly because we're using regex groups (represented by parantheses).

$re = [regex] "(<m_resultsRepositoryConnectString.*?>).*?(</m_resultsRepositoryConnectString>)"

The next four line of the program are effectively one line of script but we've spread it across multiple lines to keep things more readable. The first line is a standard dir command to scan a folder ($dir) for files matching "*.trx". Note that we specify -recurse here which means it will acutally scan sub-folders too. Simply remove this switch if you don't want to do this. Finally, we pipe the results of our dir command into a foreach; I described how to use this in my previous post: Tonight PowerShell saved my music library

dir $dir *.trx -recurse | foreach {

Next we read the contents of the file into a $in variable

$in = [System.IO.File]::ReadAllText($_.FullName)

And then we use the regex to replace the connection string. Note how I re-match the groups using the $1 and $2 tokens in the regex strings. It took me a while to get this working because the $ symbol is a special syntax to powershell and must be escaped using the ` symbol (very top left key on most UK keyboards).

$out = $re.Replace($in, "`$1" + $connectionstring + "`$2")

Finally, we have to write the results of our replace back into the original file.

[System.IO.File]::WriteAllText($_.FullName, $out)


All that remains is to close the foreach and we're done.

}

Sweeet.

 
Josh Post By Josh Twist
8:01 AM
22 Sep 2007

» Next Post: Fluent Interfaces Fnar
« Previous Post: PowerShell saves the day again

Comments are closed for this post.

Posted by Josh @ 30 Jun 2008 7:48 AM
If you want to do this in VS2008 the script needs to be modified:

$dir = "G:\HBOS Lab\CmsWebTests\TestResults - Copy"
$key = get-itemproperty HKCU:Software\Microsoft\VisualStudio\9.0\EnterpriseTools\QualityTools\Controller
$connectionstring = $key.LoadTestResultsConnectString
$re = [regex] "(resultsRepositoryConnectString=`").*?(`")"

dir $dir *.trx -recurse | foreach {
$in = [System.IO.File]::ReadAllText($_.FullName)
$out = $re.Replace($in, "`$1" + $connectionstring + "`$2")
[System.IO.File]::WriteAllText($_.FullName, $out)
}

© 2005 - 2014 Josh Twist - All Rights Reserved.