Public/Format-Chronometer.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


function Format-Chronometer
{
    <#
        .Description
        Generates a report from a Chronometer

        .Example
        $script = ls C:\workspace\PSGraph\PSGraph -Recurse -Filter *.ps1
        $resultes = Get-Chronometer -Path $script.fullname -ScriptBlock {Invoke-Pester C:\workspace\PSGraph}
        $results | Format-Chronometer -WarnAt 20 -ErrorAt 200
    #>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost","")]
    [cmdletbinding(DefaultParameterSetName='Script')]
    param(
        # This is a MonitoredScript object from Get-Chronometer
        [Parameter(
            ValueFromPipeline=$true,
            ParameterSetName='Script'
        )]
        [MonitoredScript[]]
        $InputObject,

         # This is a ScriptLine object from a MonitoredScript object
        [Parameter(
            ValueFromPipeline=$true,
            ParameterSetName='Line'
        )]
        [ScriptLine[]]
        $Line,

        # If the average time of a command is more than this, the output is yellow
        [int]
        $WarningAt = 20,

        #If the average time of a comamand is more than this, the output is red
        [int]
        $ErrorAt = 200,

        # Forces the report to show scripts with no execution time
        [switch]
        $ShowAll
    )

    process
    {
        foreach($script in $InputObject)
        {            
            if($script.ExecutionTime -ne [TimeSpan]::Zero -or $ShowAll)
            {
                Write-Host ''
                Write-Host "Script: $($script.Path)" -ForegroundColor Green
                Write-Host "Execution Time: $($script.ExecutionTime)" -ForegroundColor Green

                $script.line | Format-Chronometer -WarningAt $WarningAt -ErrorAt $ErrorAt
            }
        }

        foreach($command in $Line)
        {
            Write-ScriptLine $command -WarningAt $WarningAt -ErrorAt $ErrorAt
        }
    }
}