Home > Software design >  Calculate elapsed time for a task in azure pipeline using inline PowerShell tasks
Calculate elapsed time for a task in azure pipeline using inline PowerShell tasks

Time:12-28

I am trying to calculate the time elapsed for a task in Azure Pipeline using PowerShell task on inline mode. Simplified setup looks like in the image as attached. The inline code is mentioned below. I am getting below specified error.

Calculating Elapsed Time...
Cannot find an overload for "op_Subtraction" and the argument count: "2".
At D:\a\_temp\ba6b91f4-ef47-4a51-8088-efc6bcda310d.ps1:5 char:1
  $duration_min = $end_time - $start_time
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
      FullyQualifiedErrorId : MethodCountCouldNotFindBest
 
##[error]PowerShell exited with code '1'.

What is the right approach to measure the time elapsed for a build task?

Pipeline Setup:

enter image description here

Code in Get Start Time inline powershell task:

$start_time = Get-Date
Write-Host "Scanning Start Timestamp: $($start_time)"

Code in Calculate Elapsed Time inline powershell task:

Write-Host "Calculating Elapsed Time..."
$end_time = Get-Date
$duration_min = $end_time - $start_time
Write-Host ("Total Time Elapsed in Minutes: ", $duration_min.TotalMinutes)

CodePudding user response:

Variables between the tasks are independent. To pass the values between the tasks, we can use the pipeline variables. In your setup, go to Variables tab and create a new variable - say scan_start_time.

In the "Get Start Time" task add another line

Write-Output "##vso[task.setvariable variable=scan_start_time]$($start_time)"

Then in the "Calculate Elapsed Time" task

$start_time = "$(scan_start_time)"
  • Related