Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
93 lines (76 sloc) 2.75 KB
function Add-Duration
Adds durations to an array of objects.
Takes an array of objects with timestamps and adds duration between each object.
.Parameter Object
Specifies the array of objects to add a duration to.
.Parameter TimeProperty
Specifies field containing a timestamp used to determine duration.
.Parameter Measure
Specifies unit of time the duration should be reported in.
.Parameter Integer
Switch to determine if duration reported should be whole numbers or include decimals.
C:\PS> Get-CMLog -Path C:\ccmeval.log | Add-Duration -Measure Milliseconds | Format-Table DateTime, TotalMilliseconds, Component, Message -AutoSize
DateTime TotalMilliseconds Component Message
---- ----------------- --------- -------
07-28-2012 15:33:21.368 63 CcmEval MP check succeeded
01-21-2016 15:33:21.431 - CcmEval Send previous report if needed.
Thanks to Nasir Zubair (@nsr81) for helping with some performance optimization.
$TimeProperty = "DateTime",
$Measure = "Seconds",
$IsFirstItem = $true
$PreviousItem = $null
# Add the duration property to each object as it passes through
$Object | Add-Member -NotePropertyName "Total$($Measure)" -NotePropertyValue "-" -Force
# If this is the first object, store the current object so that when the next one is passed through, we can compare the two
if ($IsFirstItem)
$IsFirstItem = $false
$PreviousItem = $Object
# Create the duration property - Allow the invoker to specify [Int] to format the value
$Duration = New-TimeSpan -Start ($PreviousItem."$($TimeProperty)") -End ($Object."$($TimeProperty)") | Select-Object -ExpandProperty "Total$($Measure)"
if ($Integer)
$Duration = [int]$Duration
$Duration = "-"
# Update the duration property of the current object and set the current object as the previous before we continue to next object
$PreviousItem."Total$($Measure)" = $Duration
Write-Output $PreviousItem
$PreviousItem = $Object
# The last item in the list. As it is the last item, a duration does not exist and will be shown as a dash (-).
Write-Output $PreviousItem