首页 > 解决方案 > 使用参数测量启动过程的运行时间

问题描述

我有一个运行以下命令的脚本:

$result = Start-Process -Wait -PassThru $proc $args

我想测量需要多长时间。基于这篇文章,我正在尝试以下内容:

$buildtime = measure-command -expression { $result = Start-Process -Wait -PassThru $proc $args }

不幸的是,它会引发以下错误:

Start-Process : Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of
the argument collection contains a null value. Supply a collection that does not contain any null values and then try
the command again.
At C:\!scripts\pullandbuild.ps1:78 char:90
+ ... d -expression { $result = Start-Process -Wait -PassThru $proc $args }
+                                                                   ~~~~~
    + CategoryInfo          : InvalidData: (:) [Start-Process], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCommand

由于我在字符串和变量扩展方面遇到的其他问题,$args定义如下:

$args = @"
& -quit -batchmode -logFile "$logfile" -buildWindows64Player "$($fullpath)\Project.exe"
"@

我怎样才能解决这个问题?还是有另一种方法来捕获脚本的运行时?我正在尝试学习 PS,所以我宁愿避免在运行它之前以及运行和比较之后捕获时间,但如果这是我唯一的选择,我会的。

标签: powershellparameter-passing

解决方案


编辑:我刚刚检查过,你的 $args 变量是空的,原来 $args 是一个内置变量。一旦我更改了变量名,它就不再为空了,试试吧。

您如何尝试以下方法之一:

1.

$startTime = Get-Date
$processToRun = Start-Process notepad.exe -PassThru -Wait
$finishTime = Get-Date
$timeDifference = New-TimeSpan -Start $startTime -End $finishTime
  1. 使用 $result 的 StartTime 和 ExitTime 属性,然后使用 New-Timespan 比较时间

推荐阅读