forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeExpressionCommand.cs
More file actions
82 lines (63 loc) · 2.5 KB
/
Copy pathTimeExpressionCommand.cs
File metadata and controls
82 lines (63 loc) · 2.5 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
#region Using directives
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;
#endregion
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Implements a cmdlet that applies a script block
/// to each element of the pipeline.
/// </summary>
[Cmdlet(VerbsDiagnostic.Measure, "Command", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113348", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(TimeSpan))]
public sealed class MeasureCommandCommand : PSCmdlet
{
#region parameters
/// <summary>
/// This parameter specifies the current pipeline object
/// </summary>
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { set; get; } = AutomationNull.Value;
/// <summary>
/// The script block to apply
/// </summary>
[Parameter(Position = 0, Mandatory = true)]
public ScriptBlock Expression { set; get; }
#endregion
#region private members
private System.Diagnostics.Stopwatch _stopWatch = new System.Diagnostics.Stopwatch();
#endregion
#region methods
/// <summary>
/// Output the timer
/// </summary>
protected override void EndProcessing()
{
WriteObject(_stopWatch.Elapsed);
} // EndProcessing
/// <summary>
/// Execute the script block passing in the current pipeline object as
/// it's only parameter.
/// </summary>
protected override void ProcessRecord()
{
// Only accumulate the time used by this scriptblock...
// As results are discarded, write directly to a null pipe instead of accumulating.
_stopWatch.Start();
Expression.InvokeWithPipe(
useLocalScope: false,
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
dollarUnder: InputObject, // $_
input: new object[0], // $input
scriptThis: AutomationNull.Value,
outputPipe: new Pipe { NullPipe = true },
invocationInfo: null);
_stopWatch.Stop();
}
#endregion
}
} // namespace Microsoft.PowerShell.Commands