0

I have the following in a PowerShell script:

cmd /c npm run build-release 
$succeeded = $LastExitCode

What I'd like to do is:

  1. Pipe the output of the cmd to a variable so it doesn't write to the host
  2. Only if $succeeded is eq $true, Write-Host the output variable from that the cmd

How can this be done?

1 Answer 1

1

Use Invoke-Expression:

$output = Invoke-Expression "cmd /c npm run build-release"
$succeeded = $LastExitCode

if($succeeded -eq 0) {
  write-output $output
}
Sign up to request clarification or add additional context in comments.

2 Comments

I wouldn't have thought you actually need Invoke-Expression for this. Does it have to do with how npm works? $results = ping 127.0.0.1 works fine for example
@Matt I think its better from a readability point of view - it's more clear your calling something external. Its also useful if the command was ever built into a variable.

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.