I have a bunch os functions in a PowerShell script binded to an alias, as:
function Run
{
# somethin that takes a while
}
set-alias r Run
I would like to measure the time of each call. To do that, I would like to use another function that creates a .NET Stopwatch
, starts it, runs the specified action, stops the Stopwatch
and prints out the time, as:
function Crono($action)
{
$sw = [Diagnostics.Stopwatch]::StartNew()
& $action
$sw.Stop()
Write-Host "> Operation done in: " + $sw.Elapsed;
}
set-alias r Crono(Run)
But this alias throws the following error:
Set-Alias : A positional parameter cannot be found that accepts argument 'System.Object[]'. At ...\script.ps1:85 char:10 + set-alias <<<< r Crono(Run) + CategoryInfo : InvalidArgument: (:) [Set-Alias], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand
Is it possible to do that? Is there any other way?
Thanks.
Edit:
Thank you all for the replies, with the feedback received I created the following:
function CronoRun()
{
Crono(Run);
}
set-alias r **CronoRun**
But now, if the Run
function is calling a .bat
file it throws some exceptions because is trying to "interpreet" whats going on in the script, probably related to how the Run
method is exeuted inside the Crono
one. Any idea?