对从 PowerShell 运行的 NPM 命令中的错误采取措施



我有一个从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
}

注意:我对扩展做了一个假设,使其在执行时更加精细

最新更新