PowerShell定时脚本

问题描述 投票:0回答:1

试图从PS脚本中获取以下输出(如下所示),并创建另一个输出文件,说明每一步完成所需的时间。时间的格式为日:时:分:秒。

Current output:
[Cpu 1] at [Step 1] completed at 120:15:12:15
[Cpu 1] at [Step 2] completed at 120:15:17:18
[Cpu 1] at [Step 3] completed at 120:16:37:13

由于步骤2完成于120:15:17:18,所以总时间将由120:15:17:18 - 120:15:12:15.步骤3完成于120:16:37:13,所以总时间将由120:16:37:13 - 120:15:17:18。

Desired output: 
'Step 2 took xx:xx:xx:xx to complete'
'Step 3 took xx:xx:xx:xx to complete'
windows powershell
1个回答
1
投票

使用...

测量-命令

# Examples
Measure-Command { Get-ChildItem -Path C:\Windows\*.txt -Recurse }

Measure-Command {Get-ChildItem C:\Windows -Filter "*.txt" -Recurse}

10, 20, 50 | Measure-Command -Expression { for ($i=0; $i -lt $_ i++) {$i} }

10, 20, 50 | Measure-Command -Expression {for ($i=0; $i -lt $_; $i++) {$i}; "$($_)" | Out-Default }

... 并使用TotalMilliseconds属性作为结果。

或者在你的代码块中使用秒表来获得经过的时间。

# Instantiate and start a new stopwatch
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

秒表类

Stopwatch.StartNew方法

PowerShell - 测量脚本执行时间

© www.soinside.com 2019 - 2024. All rights reserved.