简体   繁体   中英

Create alias in powershell to measure the time of a function

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?

You can measure the time it took for expressions, script blocks and cmdlets with the 'Measure-Command' cmdlet.

You can also check the *ExecutionTime properties of a history item:

PS> Get-History -Count 1 | fl *

Id                 : 31
CommandLine        : Get-History -Count 1
ExecutionStatus    : Completed
StartExecutionTime : 3/13/2013 2:50:11 PM
EndExecutionTime   : 3/13/2013 2:50:11 PM

From get-help set-alias

*These commands show how to assign an alias to a command with parameters, or even to a pipeline of many commands. You can create an alias for a cmdlet, but you cannot create an alias for a command that consists of a cmdlet and its parameters. However, if you place the command in a function or a script, then you can create a useful function or script name and you can create one or more aliases for the function or script. In this example, the user wants to create an alias for the command "set-location c:\\windows\\system32", where "set-location" is a cmdlet and "C:\\Windows\\System32" is the value of the Path parameter. To do this, the first command creates a function called "CD32" that contains the Set-Location command. The second command creates the alias "go" for the CD32 function. Then, to run the Set-Location command, the user can type either "CD32" or "go".

 PS C:\> function CD32 {set-location c:\windows\system32}
 PS C:\>set-alias go cd32*

You can only alias commands without parameters. So you'd have to create a dummy function that calls Chrono(Run) and alias that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM