如何将PSCmdlet编程方式添加到PowerShell管道? [英] How to programmatically add a PSCmdlet to a Powershell pipeline?
问题描述
我想以编程方式组装和运行包含我自己PSCmdlet 管道。
然而,管道类只允许添加字符串和命令(这是从依次字符串构造的)。
I'd like to programmatically assemble and run a pipeline containing my own PSCmdlet. However, the Pipeline class only allows to add strings and Commands (which are constructed from strings in turn).
var runspace = ...;
var pipeline = runspace.CreatePipeline();
pipeline.AddCommand("Get-Date"); // ok
var myCmdlet = new MyCmdlet();
pipeline.AddCommand(myCmdlet); // Doesn't compile - am I fundamentally
// misunderstanding some difference between commands and commandlets?
foreach(var res in pipeline.Invoke()) {...}
我相信我在做什么应该基本道理...还是有不同的方式来做到这一点?
I believe that what I'm doing should basically make sense... or is there a different way to do this?
推荐答案
您cmdlet的应该有一个明确定义的语法,根据你放什么在cmdlet属性。
Your cmdlet should have a well-defined syntax, based on what you put in the Cmdlet attribute.
举例来说,这里是哪里创建我自己的明确的主机小命令启动更换内置明确的主机功能:
For instance, here's the start of where I create my own clear-host cmdlet to replace the built-in clear-host function:
<Cmdlet("clear", "host")> _
Public Class Clearhost
Inherits Cmdlet
从该cmdlet属性,语法我的cmdlet的是明确的主持人。你应该能够使用(因为它是一个字符串),并把它添加到管道
From the Cmdlet attribute, the syntax for my cmdlet is "clear-host". You should be able to use that (since it's a string) and add it to the pipeline.
这篇关于如何将PSCmdlet编程方式添加到PowerShell管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!