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

时间:2018-09-25 15:35:48

标签: 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。

1 个答案:

答案 0 :(得分:3)

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

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

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