在PowerShell中定时执行命令 [英] Timing a command's execution in PowerShell

查看:207
本文介绍了在PowerShell中定时执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来计时PowerShell中命令的执行时间,例如Linux中的时间"命令?
我想到了这个:

Is there a simple way to time the execution of a command in PowerShell, like the 'time' command in Linux?
I came up with this:

$s=Get-Date; .\do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds

但是我想要更简单的东西

But I would like something simpler like

time .\do_something.ps1

推荐答案

是的.

Measure-Command { .\do_something.ps1 }

请注意,Measure-Command的一个小缺点是您看不到任何stdout输出.

Note that one minor downside of Measure-Command is that you see no stdout output.

[更新,感谢@JasonMArcher]您可以通过将命令输出传递到写入主机的某些Commandlet中来解决此问题. Out-Default变为:

[Update, thanks to @JasonMArcher] You can fix that by piping the command output to some commandlet that writes to the host, e.g. Out-Default so it becomes:

Measure-Command { .\do_something.ps1 | Out-Default }

查看输出的另一种方法是使用.NET Stopwatch类,如下所示:

Another way to see the output would be to use the .NET Stopwatch class like this:

$sw = [Diagnostics.Stopwatch]::StartNew()
.\do_something.ps1
$sw.Stop()
$sw.Elapsed

这篇关于在PowerShell中定时执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆