PowerShell:捕获写入变量中的 stderr 的外部进程的输出 [英] PowerShell: Capture the output from external process that writes to stderr in a variable

查看:65
本文介绍了PowerShell:捕获写入变量中的 stderr 的外部进程的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将外部进程的输出捕获到一个变量(字符串)中,以便我可以对其进行一些处理.

I need to capture the output of a external process into a variable (string), so I can do some processing on that.

来自here 只要进程写入标准输出就可以很好地工作.但是,如果进程失败,它会写入 stderr.我也想捕获这个字符串,不知道怎么做.

The answer from here works nicely as long as the process writes to stdout. However, if the process fails, it writes to stderr. I'd like to capture this string too, and I can't figure out how to do it.

示例:

$cmdOutput = (svn info) | out-string

这有效,除非 SVN 有错误.如果发生错误,SVN 写入 stderr,并且 $cmdOutput 为空.

This works, unless SVN has an error. If an error occured, SVN writes to stderr, and $cmdOutput is empty.

如何在 PowerShell 的变量中捕获写入 stderr 的文本?

How do I capture the text written to stderr in a variable in PowerShell ?

推荐答案

试试这个:

$cmdOutput = svn info 2>&1

这篇关于PowerShell:捕获写入变量中的 stderr 的外部进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆