【发布时间】:2017-01-04 11:15:56
【问题描述】:
我有 3 个用于 powershell 的命令:
shell.AddScript("Exit-PSSession");
shell.AddScript("Invoke-Command -ComputerName something -ScriptBlock {some command}");
shell.AddScript("Invoke-Command -ComputerName something -ScriptBlock {another command}");
现在我不需要前两个的任何响应,但我需要第三个的错误日志。因为我无法预见可能会发生多少错误,所以我想至少从所有错误消息中清除shell,最好是一个完全空的shell。
我的解决方案是这样的:
shell.AddScript("Exit-PSSession");
shell.AddScript("Invoke-Command -ComputerName something -ScriptBlock {some command}");
shell.Invoke();
shell.Streams.ClearStreams();
shell.AddScript("Invoke-Command -ComputerName something -ScriptBlock {another command}");
但不知何故,ClearStreams 什么都不做,shell 仍然知道所有旧错误和前两个命令。
Microsoft 方面没有提供更多信息,只是表示此方法存在并且应该清除shell。 (Microsoft Help for ClearStreams) 或Microsoft Help for streams in general
是我错过了什么,还是我误解了他们的意思
(Powershell 是 5.0 版)和 C# 运行 4.6 NET Framework
提前感谢您的帮助。
【问题讨论】:
-
ClearStreams仅清除给定PowerShell实例的流。它与附加的 PowerShellRunspace(shell.Runspace) 无关。特别是,它与$Error变量或Runspace中的历史无关。
标签: c# shell powershell