PowerShell 捕获写入主机输出 [英] PowerShell Capture Write-Host output
问题描述
我必须运行 Microsoft cmdlet,并且使用 cmdlet 中的 Write-Host 行将重要信息写入控制台.
I am having to run a Microsoft cmdlet, and the important bit of information is written to console using a Write-Host line within the cmdlet.
它没有返回,所以我不能做 $result = Commandlet ...返回了一个对我没有用的不同值,我实际需要的是在命令行开关中打印到控制台,无论如何我可以嗅探"或抓取"控制台以获取我想要的信息吗?
It is NOT returned, so I cannot do $result = Commandlet ... A different value is returned that is not of use to me, what I actually need is printed to console within the commandlet is there anyway I can 'sniff' or 'scrape' the console to get the information I want?
$result = Test-Cluser
Test-Cluster 将打印如下内容:'HadUnselectedTests'、'ClusterConditionallyApproved' 等.但是它在 .htm 报告文件的路径中返回的值.不幸的是,.htm 报告文件不包含这些状态代码之一,所以我也不能只为它解析 .htm 文件.
Test-Cluster will print stuff like: 'HadUnselectedTests', 'ClusterConditionallyApproved', etc. But the value it returns in the path to the .htm report file. And the .htm report file does not contain one of those status codes unfortunately so I cannot just parse the .htm file for it either.
有什么建议吗?
推荐答案
注意:至于 为什么永远不应该使用 Write-Host
输出数据,见这个答案.
在PSv5+中:
$result = Test-Cluster 6>&1
从版本 5 开始,Write-Host
写入新引入的信息流,其编号为 6
.
Since version 5, Write-Host
writes to the newly introduced information stream, whose number is 6
.
6>&1
将该流重定向到成功输出流(编号 1
),以便它也可以在 $result中捕获代码>.
6>&1
redirects that stream to the success output stream (number 1
), so that it too can be captured in $result
.
在PSv4-:
没有方法可以捕获Write-Host
输出会话中.
There is no way to capture Write-Host
output in-session.
唯一的解决方法是启动另一个 Powershell 实例,并将目标命令指定为字符串.
注意事项:
The only workaround is to launch another instance of Powershell with the target command specified as a string.
Caveats:
- 这样的调用很慢,
- 防止传递具有原始数据类型的参数
- 总是只返回字符串数据(文本行)
- 从所有输出流返回输出,包括错误输出
- 有关所有输出流的列表,请参阅
获取关于_重定向的帮助
- Such an invocation is slow,
- prevents passing of arguments with their original data type
- invariably only returns string data (lines of text)
- returns output from all output streams, including error output
- for a list of all output streams, see
Get-Help about_Redirection
$result = powershell -noprofile -command 'Test-Cluster'
请注意,使用 脚本块 来传递命令 (
-command { Test-Cluster }
) 将 行不通,因为 PowerShell 然后使用序列化和反序列化来模拟会话中的行为.Note that using a script block to pass the command (
-command { Test-Cluster }
) would not work, because PowerShell then uses serialization and deserialization to emulate the in-session behavior.获取帮助 about_Redirection
讨论了所有输出流的列表,可以通过它们的编号来定位;从 PSv5 开始,这些是:Get-Help about_Redirection
discusses a list of all output streams, which can be targeted by their numbers; since PSv5, these are:1 ... success output stream (implicit output and Write-Output output) 2 ... error output stream (Write-Error and unhandled errors) 3 ... warnings (Write-Warning) 4 ... verbose output (Write-Verbose) 5 ... debug output (Write-Debug) 6 ... (v5+) Write-Information and Write-Host output
请注意,某些流默认情况下是静默的,需要选择加入以通过首选项变量(例如
$VerbosePreference
)或公共参数(例如-Verbose
)Note that some streams are silent by default and require opt-in to produce output, either via a preference variable (e.g.,
$VerbosePreference
) or a common parameter (e.g.,-Verbose
){n}>
允许重定向数字{n}
流;如果省略{n}
,则隐含1
:
{n}>
allows redirecting the number{n}
stream; if{n}
is omitted,1
is implied:
- 到一个文件(例如,
3> c:/tmp/warnings.txt
- 到无处",即抑制输出(例如,
3> $null
) - 到成功输出流(例如,
3>&1
);注意:只有流1
可以通过这种方式定位.
- to a file (e.g.,
3> c:/tmp/warnings.txt
- to "nowhere", i.e suppressing the output (e.g.,
3> $null
) - to the success output stream (e.g.,
3>&1
); note: only stream1
can be targeted this way.
*>
目标 所有 输出流.注意:与 POSIX 类 shell(例如,
bash
)不同,multiple 重定向表达式的 order 不 很重要.Note: Unlike in POSIX-like shells (e.g.,
bash
), the order of multiple redirection expression does not matter.因此,以下类似 POSIX 的 shell 习惯用法 - 将错误输出重定向到成功流并仅使 原始 成功输出静音 - 不起作用:
Therefore, the following POSIX-like shell idiom - which redirects error output to the success stream and silences only the original success output - does NOT work:
... 2>&1 1>$null # !! NO output in PowerShell
要在 PowerShell 中实现这一点,您不能重定向
1
,而是通过源流过滤成功的对象.To achieve this in PowerShell, you mustn't redirect
1
and instead filter the objects in the success by their stream of origin.举个例子:最后,OP 想要以下内容:只捕获警告输出,没有常规(成功)输出:
Case in point: In the end, the OP wanted the following: capture only warning output, without the regular (success) output:
Test-Cluster 3>&1 | Where-Object { $_ -is [System.Management.Automation.WarningRecord] }
来自警告流的对象具有
[System.Management.Automation.WarningRecord]
类型,这是启用上述过滤的原因.Objects that came from the warning stream have type
[System.Management.Automation.WarningRecord]
, which is what enables the filtering above.这篇关于PowerShell 捕获写入主机输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- for a list of all output streams, see
- 有关所有输出流的列表,请参阅