以分钟和秒为单位衡量执行时间

use*_*917 3 powershell

我想在自动化PowerShell脚本中进行多次测量。我使用了Get-Date和TotalSeconds。结果看起来像这样:236.908

如何以分钟和秒为单位获得经过的时间?

$startTime = (Get-Date)
$endTime = (Get-Date)

$ElapsedTime = (($endTime-$startTime).TotalSeconds)

Write-Host "Duration: xx min xx sec"
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 8

使用格式运算符-f):

'Duration: {0:mm} min {0:ss} sec' -f ($endTime-$startTime)
Run Code Online (Sandbox Code Playgroud)

或类似这样,如果您还需要其他地方的区别:

$ElapsedTime = $endTime-$startTime
'Duration: {0:mm} min {0:ss} sec' -f $ElapsedTime
Run Code Online (Sandbox Code Playgroud)


Esp*_*o57 6

您也可以使用Measure-Command

Measure-Command -Expression {

    # Command 1
    Get-ChildItem

    # Command N
    Get-Process
}
Run Code Online (Sandbox Code Playgroud)

或者像这样:

$result = Measure-Command -Expression {

    # Command 1
    Get-ChildItem

    # Command N
    Get-Process
}

$result.ToString()
Run Code Online (Sandbox Code Playgroud)