代码之家  ›  专栏  ›  技术社区  ›  MrHinsh - Martin Hinshelwood

对从PowerShell运行的NPM命令中的错误执行操作

  •  2
  • MrHinsh - Martin Hinshelwood  · 技术社区  · 7 年前

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

    但是美元?返回的是真的,$lastexitcode是1。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Maximilian Burszley    7 年前

    您需要捕获应用程序的输出 第一 :

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

    注意:我做了一个关于扩展的假设,使其与执行更加细化。