PowerShell 旨在支持和支持流式输出样式(输出和用户反馈在生成时可用,而不是在脚本完成时被转储)。PowerShell 提供了各种Write
cmdlet、它们的流和它们的重定向功能来启用这种风格。
但是,当使用Invoke-Command
时,在远程 PSSession 中操作时的流重定向行为取决于所使用的流。
例如:
# Information redirect works as expected when invoking locally
>Invoke-Command { 'output'; Write-Host 'info' } 6>$Null
output
# Information redirect gets ignored when invoking remotely
# (assuming a remote session has already been created as $session)
>Invoke-Command -Session $session { 'output'; Write-Host 'info' } 6>$Null
output
info
(Write-Host
为简单起见,使用相同的行为发生Write-Information -InformationAction Continue
)
实验表明流的行为因流而异:
- 成功 (1) 和错误 (2) 正常输出重定向
- 警告 (3) 和详细 (4) 输出无条件类似于信息 (6),但它们的格式被保留
- 调试 (5) 正常重定向(尽管继续提示不重定向)
我可以看到一些自定义方法来陪审团流输出,随后可以轻松过滤(例如,使用识别前缀输出到 Success)。
有没有一种好方法可以从远程命令中流式传输单独的输出和反馈,这将遵守某种形式的 PowerShell 常见做法,或者有一种方法可以让重定向按预期运行?