在PowerShell中定时执行命令

Pao*_*sco 198 powershell time

有没有一种简单的方法来计算PowerShell中命令的执行时间,比如Linux中的'time'命令?
我想出了这个:

$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds
Run Code Online (Sandbox Code Playgroud)

但我想要更简单的东西

time .\do_something.ps1
Run Code Online (Sandbox Code Playgroud)

Kei*_*ill 309

对.

Measure-Command { .\do_something.ps1 }
Run Code Online (Sandbox Code Playgroud)

请注意,Measure-Command的一个小缺点是您没有看到stdout输出.如果要查看输出,则可以使用.NET Stopwatch对象,例如:

$sw = [Diagnostics.Stopwatch]::StartNew()
.\do_something.ps1
$sw.Stop()
$sw.Elapsed
Run Code Online (Sandbox Code Playgroud)

  • 您还可以看到这样的输出,Measure-Command {ps | 的Out-Default}.或者直接写入主机的任何其他内容,可能有用也可能没用. (107认同)
  • 我采用了这个解决方案并编写了一个对其他人有用的函数.https://gist.github.com/2206444 - 例如:`time {ping -n 1 google.com} -Samples 10`将运行命令`10`次并返回平均,最短和最长时间.你可以添加`-Silent`来吞下STDOUT. (17认同)
  • 我倾向于将Measure-Command的结果赋给变量,例如`$ t = Measure-Command {<< your command or code block >>}`.试试看,然后在提示符下键入`$ t`,看看你的结果和你有权访问的所有属性,比如`$ t.Milliseconds`,`$ t.TotalSeconds`等.然后我们可以写任何输出例如,我们想要`Write-Host那个命令花了$ t.TotalSeconds来完成 (11认同)
  • 类似于`| Out-Default`,也可以使用`-OutVariable`来存储输出对象,例如`$m = Measure-Command { Get-WinEvent -LogName System -MaxEvents 1 -OutVariable events };$events.消息;$m.TotalSeconds` (3认同)

Sha*_*evy 172

您还可以从历史记录中获取最后一个命令并从中减去EndExecutionTimeStartExecutionTime.

.\do_something.ps1  
$command = Get-History -Count 1  
$command.EndExecutionTime - $command.StartExecutionTime
Run Code Online (Sandbox Code Playgroud)

  • 试试这个:`Get-History | 组{$ _.StartExecutionTime.Hour} | 排序计数-desc`以按小时查看PowerShell使用模式.:-) (22认同)
  • 能够使用它来找出事情花了多长时间,即使你不期望它在你开始时花费很长时间,所以你不想将它包装在Measure-Command中. (17认同)
  • 重要提示,我不知道这在 '10 中有多真实,但在此评论之前的某个时刻,命令对象有一个“持续时间”字段,因此不再需要从两个 FooExecutionTime 字段手动计算。 (4认同)
  • 使用持续时间的一行:`Get-History -Count 1 | %{ $_. 持续时间 }` (4认同)
  • powershell有时很棒. (3认同)
  • 是的,这太棒了!我做了一个单行使用:``$command = Get-History -Count 1 ; "{0}" -f ($command.EndExecutionTime - $command.StartExecutionTime)`` (3认同)

Dro*_*roj 99

使用 Measure-Command

Measure-Command { <your command here> | Out-Host }
Run Code Online (Sandbox Code Playgroud)

管道Out-Host允许您查看命令的输出,否则将被消耗Measure-Command.

  • @Peter - 它需要位于块内,否则 Measure-Command 在输出到达控制台之前会消耗它。 (3认同)
  • 由于与脚本兼容,Out-Default 可能比 Out-Host 更好吗?https://www.jsnover.com/blog/2013/12/07/write-host-thinked-harmful/ (2认同)

小智 17

Simples

function time($block) {
    $sw = [Diagnostics.Stopwatch]::StartNew()
    &$block
    $sw.Stop()
    $sw.Elapsed
}
Run Code Online (Sandbox Code Playgroud)

然后可以用作

time { .\some_command }
Run Code Online (Sandbox Code Playgroud)

您可能想要调整输出

  • `Measure-Command` 隐藏命令输出,所以这个解决方案有时更好。 (2认同)

Ben*_*est 7

这是我编写的一个与 Unixtime命令类似的函数:

function time {
    Param(
        [Parameter(Mandatory=$true)]
        [string]$command,
        [switch]$quiet = $false
    )
    $start = Get-Date
    try {
        if ( -not $quiet ) {
            iex $command | Write-Host
        } else {
            iex $command > $null
        }
    } finally {
        $(Get-Date) - $start
    }
}
Run Code Online (Sandbox Code Playgroud)

来源:https : //gist.github.com/bender-the-greatest/741f696d965ed9728dc6287bdd336874

  • 这是我编写的 Powershell 函数,它展示了如何自己计算执行时间,而不是使用“Measure-Command”或您可以在 Powershell 中为执行计时的其他各种方法之一。如果你阅读了最初的问题,他会要求一些“类似于 Linux 中的‘time’命令”的东西。 (6认同)

小智 7

一种更受 PowerShell 启发的方式来访问您关心的属性的价值:

$myCommand = .\do_something.ps1
Measure-Command { Invoke-Expression $myCommand } | Select -ExpandProperty Milliseconds
Run Code Online (Sandbox Code Playgroud)
    4
Run Code Online (Sandbox Code Playgroud)

由于Measure-Command返回一个TimeSpan对象。

注意:TimeSpan 对象还具有双精度型 TotalMilliseconds(例如上面我的例子中的 4.7322 TotalMilliseconds),这可能对您有用。就像 TotalSeconds、TotalDays 等。


Joh*_*man 5

到目前为止,所有答案都达不到提问者(和我)通过简单地在命令行开头添加“time”来计时命令的愿望。相反,它们都需要将命令括在方括号 ( {}) 中以形成一个块。下面是一个简短的函数,它的工作方式更像是time在 Unix 上:

Function time() {
  $command = $args -join ' '
  Measure-Command { Invoke-Expression $command | Out-Default }
}
Run Code Online (Sandbox Code Playgroud)