3

I have an NPM command that I am running from PowerShell:

npm install process-migrator -g 
process-migrator 

This command returns an error when its not configured properly, but I cant seam to pick it up in 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.

Is there some way to get the error?

I have tried:

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

But $? is returning true and $LASTEXITCODE is a 1.

0

1 Answer 1

5

You will need to capture the output of the application 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
}

Note: I made an assumption about the extension to be more granular with the execution

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain what the first & sign means after the $output =?
For future visitors: I added the following line within the if statement to pass the exit code to the poweshell exit like that: exit $LASTEXITCODE;
@BrunoBieri Special operators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.