简体   繁体   English

在PowerShell中将cmd输出管道传输到变量

[英]Piping cmd output to variable in PowerShell

I have the following in a PowerShell script: 我在PowerShell脚本中具有以下内容:

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 cmd的输出通过管道传递给变量,这样它就不会写入主机
  2. Only if $succeeded is eq $true , Write-Host the output variable from that the cmd $succeededeq $true ,才从该cmd Write-Host输出变量

How can this be done? 如何才能做到这一点?

Use Invoke-Expression : 使用Invoke-Expression

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

if($succeeded -eq 0) {
  write-output $output
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM