gpt4 book ai didi

powershell - 处理从PowerShell运行的NPM命令中的错误

转载 作者:行者123 更新时间:2023-12-02 23:20:17 25 4
gpt4 key购买 nike

我有一个从PowerShell运行的NPM命令:

npm install process-migrator -g 
process-migrator

如果未正确配置此命令,则该命令将返回错误,但是我无法在PowerShell中进行拾取。
[ERROR] [2018-09-25T15:30:30.610Z] Cannot find configuration file 'configuration.json'
[INFORMATION] [2018-09-25T15:30:30.615Z] Generated configuration file as 'configuration.json', please fill in required information and retry.

是否有某种方法可以得到该错误?

我努力了:
if($?) {            
Write-Host "Process-Migrator completed sucessfully." $LASTEXITCODE
} else {
Write-VstsTaskError "Process-Migrator FAILED " $LASTEXITCODE -ErrCode $LASTEXITCODE
}
Process-Migrator completed sucessfully. 1

但是$?返回true,$ LASTEXITCODE为1。

最佳答案

您将需要首先捕获应用程序的输出:

$output = & 'process-migrator.exe' 2>&1
if ($LASTEXITCODE -ne 0)
{
$err = $output.Where{$PSItem -match 'ERROR'}
Write-VstsTaskError "Process-Migrator FAILED: $err" -ErrCode $LASTEXITCODE
}

注意:我对扩展进行了假设,以使执行更加精细

关于powershell - 处理从PowerShell运行的NPM命令中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52501935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com