powershell - 使用参数测量启动过程的运行时间
问题描述
我有一个运行以下命令的脚本:
$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,所以我宁愿避免在运行它之前以及运行和比较之后捕获时间,但如果这是我唯一的选择,我会的。
解决方案
编辑:我刚刚检查过,你的 $args 变量是空的,原来 $args 是一个内置变量。一旦我更改了变量名,它就不再为空了,试试吧。
您如何尝试以下方法之一:
1.
$startTime = Get-Date
$processToRun = Start-Process notepad.exe -PassThru -Wait
$finishTime = Get-Date
$timeDifference = New-TimeSpan -Start $startTime -End $finishTime
- 使用 $result 的 StartTime 和 ExitTime 属性,然后使用 New-Timespan 比较时间
推荐阅读
- javascript - 无法更改画布文本的颜色
- c# - 初始化集合属性好不好
- android - 当键盘退格键按下Android时,我如何将光标从一个编辑文本移动到另一个
- date - SAS PROC SQL - 如何将月份添加到日期
- javascript - 在不使用库的情况下查找用户定义的回溯日期
- java - 为什么这段代码显示 StackOverflow 错误?
- javascript - Cosmos DB MongoDB API 无法调用存储过程/UDF/
- ruby - 在厨师食谱中组织食谱
- c# - 填充到下拉列表中
- output-formatting - 如何更改 Niftynet 的输出格式?