加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何从C#以编程方式调用CmdLet时捕获Powershell CmdLet的Object

发布时间:2020-12-15 21:57:08 所属栏目:百科 来源:网络整理
导读:背景 带有SDK的PowerShell 3 C#with Framework 4.5 履行 我用PSHostUserInterface实现了我自己的PSHost alogn. 在PSHostUserInterface中,我覆盖所有Write …方法并收集一个为输出提供服务的变量. 问题 在我的应用程序中,我调用了使用WriteObject作为输出的Cm
背景

>带有SDK的PowerShell 3
> C#with Framework 4.5

履行

我用PSHostUserInterface实现了我自己的PSHost alogn.
在PSHostUserInterface中,我覆盖所有Write …方法并收集一个为输出提供服务的变量.

问题

在我的应用程序中,我调用了使用WriteObject作为输出的Cmdlet.在PSHostUserInterface.Write …方法中,除了这些WriteObject的输出,我得到了所有东西.
例如,我在常规PowerShell中看到了这一点:

This is sample string output from the command

Here we have object from Cmdlet.WriteObject function

This is another string output from the command

这是我在我的应用程序中的自定义PSHost中得到的:

This is sample string output from the command

This is another string output from the command

如何在C#中获取Cmdlet的所有输出?

非常感谢

解决方法

我认为理查德正在发表评论.如果使用Pipeline.Invoke()调用cmdlet,则必须:

> A)调用Pipeline.Invoke再次使用这些对象并将它们输入Out-Default或
> B)只需将命令Out-Default附加到原始管道.这将告诉PowerShell将输出发送到显示器(并使用默认格式).

通常,当您存储由管道输出的结果对象时,无论是在PowerShell变量$res = Get-Process还是在C#中作为Invoke()的输出,您都在处理实际的.NET对象.格式化&将这些对象呈现给主机是PowerShell在未捕获管道输出时将为您执行的另一个步骤. PowerShell有效地附加了一个| Out-在这种情况下默认为管道.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读