[英]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: 我想做的是:
cmd
to a variable so it doesn't write to the host 将cmd
的输出通过管道传递给变量,这样它就不会写入主机 $succeeded
is eq $true
, Write-Host
the output variable from that the cmd
仅当$succeeded
为eq $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.