2016-03-30 14:20:52 -07:00
|
|
|
/********************************************************************++
|
|
|
|
|
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>
|
2016-08-23 21:21:31 -04:00
|
|
|
[Cmdlet(VerbsDiagnostic.Measure, "Command", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113348", RemotingCapability = RemotingCapability.None)]
|
2016-03-30 14:20:52 -07:00
|
|
|
[OutputType(typeof(TimeSpan))]
|
|
|
|
|
public sealed class MeasureCommandCommand : PSCmdlet
|
|
|
|
|
{
|
|
|
|
|
#region parameters
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This parameter specifies the current pipeline object
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
[Parameter(ValueFromPipeline = true)]
|
2016-08-02 13:29:09 -07:00
|
|
|
public PSObject InputObject { set; get; } = AutomationNull.Value;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The script block to apply
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(Position = 0, Mandatory = true)]
|
2016-08-02 13:29:09 -07:00
|
|
|
public ScriptBlock Expression { set; get; }
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region private members
|
|
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
private System.Diagnostics.Stopwatch _stopWatch = new System.Diagnostics.Stopwatch();
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Output the timer
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void EndProcessing()
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
WriteObject(_stopWatch.Elapsed);
|
2016-03-30 14:20:52 -07:00
|
|
|
} // 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.
|
2016-07-29 13:02:49 -07:00
|
|
|
_stopWatch.Start();
|
2016-08-02 13:29:09 -07:00
|
|
|
Expression.InvokeWithPipe(
|
2016-07-29 13:02:49 -07:00
|
|
|
useLocalScope: false,
|
|
|
|
|
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
|
|
|
|
|
dollarUnder: InputObject, // $_
|
|
|
|
|
input: new object[0], // $input
|
|
|
|
|
scriptThis: AutomationNull.Value,
|
|
|
|
|
outputPipe: new Pipe { NullPipe = true },
|
|
|
|
|
invocationInfo: null);
|
2016-03-30 14:20:52 -07:00
|
|
|
|
2016-07-29 13:02:49 -07:00
|
|
|
_stopWatch.Stop();
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
} // namespace Microsoft.PowerShell.Commands
|
|
|
|
|
|