Powershell to help plot graphs of how long TFS upgrades take

When doing TFS upgrades it is useful to know roughly how long they will take. The upgrade programs give a number of steps, but not all steps are equal. Some are quick, some are slow. I have found it useful to graph past updates so I can get a feel of how long an update will take given it got to ‘step x in y minutes’. You can do this by hand, noting down time as specific steps are reached. However for a long upgrade it usually means pulling data out of the TFS TPC upgrade logs.

To make this process easier I put together this script to find the step completion rows in the log file and format them out such that they are easy to graph in Excel

 1param  
 2(  
 3    $logfile = "TPC\_ApplyPatch.log",  
 4    $outfile = "out.csv"  
 5) 
 6
 7  
 8 # A function to covert the start and end times to a number of minutes  
 9 # Can't use simple timespan as we only have the time portion not the whole datetime  
10 # Hence the hacky added a day-1 second  
11 function CalcDuration  
12 {  
13    param  
14    (  
15        $startTime,  
16        $endTime  
17    )
18
19 
20
21    $diff = \[dateTime\]$endTime - $startTime  
22    if (\[dateTime\]$endTime -lt $startTime)   
23    {   
24       $diff += "23:59" # add a day as we past midnight  
25    }
26
27 
28
29    \[int\]$diff.Hours \*60 + $diff.Minutes  
30 }
31
32 
33
34 Write-Host "Importing $logfile for processing"  
35 # pull out the lines we are interested in using a regular expression to extract the columns  
36 # the (.{8} handle the fixed width, exact matches are used for the test  
37 $lines = Get-Content -Path $logfile | Select-String "  Executing step:"  | Where{$\_ -match "^(.)(.{8})(.{8})(Executing step:)(.{2})(.\*)(')(.\*)(\[(\])(.\*)(\[ \])(\[of\])(.\*)"} | ForEach{  
38    \[PSCustomObject\]@{  
39        'Step' = $Matches\[10\]  
40        'TimeStamp' = $Matches\[2\]  
41        'Action' = $Matches\[6\]  
42    }  
43 }  
44   
45\# We assume the upgrade started at the timestamp of the 0th step  
46\# Not true but very close  
47\[DateTime\]$start = $lines\[0\].TimeStamp
48
49 
50
51Write-Host "Writing results to $outfile"  
52\# Work out the duration  
53 $steps = $lines | ForEach{  
54    \[PSCustomObject\]@{  
55        'Step' = $\_.Step  
56        'TimeStamp' = $\_.TimeStamp  
57        'EplasedTime' = CalcDuration -startTime $start -endTime $\_.TimeStamp   
58        'Action' = $\_.Action  
59          
60    }  
61 }   
62 $steps | export-csv $outfile -NoTypeInformation 
63
64 
65
66\# and list to screen  
67$steps