Functions/Public/Get-Stopwatch.ps1
|
function Get-Stopwatch { <# .SYNOPSIS Gets the current state or results of named stopwatches. .DESCRIPTION Retrieves information about running stopwatches or completed stopwatch results. Can return all stopwatches or filter by name. .PARAMETER Name Optional. The name of a specific stopwatch to retrieve. Supports wildcards. If not specified, returns all stopwatches. .PARAMETER Running If specified, returns only currently running stopwatches (not yet stopped). .PARAMETER Completed If specified, returns only completed stopwatch results (already stopped). .OUTPUTS PSCustomObject with stopwatch information. .EXAMPLE Get-Stopwatch # Returns all completed stopwatch results .EXAMPLE Get-Stopwatch -Name "API*" # Returns all stopwatches matching "API*" .EXAMPLE Get-Stopwatch -Running # Returns all currently running stopwatches .EXAMPLE Get-Stopwatch -Name "Build" -Completed # Returns the completed result for "Build" stopwatch .NOTES Author: Sune Alexandersen Narud Version: 1.0.0 Date: February 2026 .LINK Start-Stopwatch Stop-Stopwatch Show-StopwatchSummary #> [CmdletBinding(DefaultParameterSetName = 'All')] [OutputType([PSCustomObject[]])] param( [Parameter(Position = 0, ValueFromPipeline = $true)] [SupportsWildcards()] [string]$Name = '*', [Parameter(ParameterSetName = 'Running')] [switch]$Running, [Parameter(ParameterSetName = 'Completed')] [switch]$Completed ) process { $results = @() # Get running stopwatches if ($Running -or (-not $Completed -and -not $Running)) { if ($Global:OrionStopwatches) { $now = [DateTime]::Now foreach ($key in $Global:OrionStopwatches.Keys) { if ($key -like $Name) { $startTime = $Global:OrionStopwatches[$key] $elapsed = $now - $startTime $results += [PSCustomObject]@{ Name = $key Status = 'Running' StartTime = $startTime EndTime = $null Duration = $elapsed TotalSeconds = $elapsed.TotalSeconds } } } } } # Get completed stopwatches if ($Completed -or (-not $Completed -and -not $Running)) { if ($Global:OrionStopwatchResults) { foreach ($key in $Global:OrionStopwatchResults.Keys) { if ($key -like $Name) { $result = $Global:OrionStopwatchResults[$key] $results += [PSCustomObject]@{ Name = $result.Name Status = 'Completed' StartTime = $result.StartTime EndTime = $result.EndTime Duration = $result.Duration TotalSeconds = $result.TotalSeconds } } } } } # Return sorted by TotalSeconds descending return $results | Sort-Object TotalSeconds -Descending } } |