Exporting VSTS load test results to a new database with PowerShell
Posted by ~Ray @ 2007-12-09 14:14:37
express emotion readers of theJoyOfCode com ordain 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 show the use of powershell for something a little bit more complicated than my previous and admittedely very simple examples.
The label sample colin provides basically traverses a directory looking for trx files (created by VSTS load evaluate) and swaps the connection arrange 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. register]::ReadAllText($_. FullName) $out = $re. regenerate($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 act a look at it line-by-line to see how it works:
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 determine of the connection arrange 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. LoadTestResultsConnectStringNow we create a regular expression that will scan the trx files and find the appropriate area to replace. say 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 lie of the program are effectively one lie of script but we've spread it across multiple lines to keep things more readable. The first line is a standard dir command to examine a folder ($dir) for files matching "* trx". Note that we specify -recurse here which means it will acutally examine sub-folders too. Simply remove this switch if you don't be to do this. Finally we pipe the results of our dir dominate into a foreach; I described how to use this in my previous post: dir $dir * trx -recurse | foreach {Next we construe 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. [ADVERTHERE]Related article:
http://www.thejoyofcode.com/Exporting_VSTS_load_test_results_to_a_new_database_with_PowerShell.aspx
0 Comments:
No comments have been posted yet!
|