Skip to content

Instantly share code, notes, and snippets.

@rfennell
Last active October 5, 2023 10:44
Show Gist options
  • Save rfennell/d956e3a488850cd492f19026ca5ba291 to your computer and use it in GitHub Desktop.
Save rfennell/d956e3a488850cd492f19026ca5ba291 to your computer and use it in GitHub Desktop.
Extracts the timestamps from a TFS/Azure DevOps upgrade log or ease of charting
param
(
$logfile = "TPC\_ApplyPatch.log",
$outfile = "out.csv"
)
# A function to covert the start and end times to a number of minutes
# Can't use simple timespan as we only have the time portion not the whole datetime
# Hence the hacky added a day-1 second
# The log file only records the time, not the date, so if a step is over 24hrs it is wrong
# An alternate would be to find the elapsed from the text, but will require REGEX
# Not done as such long steps are rare
# Step passed: Upgrade Version Control database. Execution time: 24 hours and 2 minutes.
function CalcDuration
{
param
(
$startTime,
$endTime
)
$diff = [dateTime]$endTime - $startTime
if ([dateTime]$endTime -lt $startTime)
{
$diff += "23:59" # add a day as we past midnight
}
[int]$diff.Hours *60 + $diff.Minutes
}
Write-Host "Importing $logfile for processing"
# pull out the lines we are interested in using a regular expression to extract the columns
# the (.{8} handle the fixed width, exact matches are used for the test
$lines = Get-Content -Path $logfile | Select-String " Executing step:" | Where{$_ -match "^(.)(.{8})(.{8})(Executing step:)(.{2})(.*)(')(.*)([(])(.*)([ ])([of])(.*)"} | ForEach{
[PSCustomObject]@{
'Step' = $Matches[10]
'TimeStamp' = $Matches[2]
'Action' = $Matches[6]
}
}
# We assume the upgrade started at the timestamp of the 0th step
# Not true but very close
[DateTime]$start = $lines[0].TimeStamp
Write-Host "Writing results to $outfile"
# Work out the duration
$steps = $lines | ForEach{
[PSCustomObject]@{
'Step' = $_.Step
'TimeStamp' = $_.TimeStamp
'EplasedTime' = CalcDuration -startTime $start -endTime $_.TimeStamp
'Action' = $_.Action
}
}
$steps | export-csv $outfile -NoTypeInformation
# and list to screen
$steps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment