简体   繁体   English

C#中的Pipeline和PowerShell类之间的区别

[英]Difference Between Pipeline and PowerShell class in C#

I want to know the difference between executing PowerShell script in C# using the Pipeline class versus the PowerShell class. 我想知道使用Pipeline类与PowerShell类在C#执行PowerShell脚本之间的区别。

Using Pipeline : 使用管道:

Pipeline pipe = runspace.CreatePipeline();

Using PowerShell class: 使用PowerShell类:

PowerShell ps = PowerShell.Create();

We can use both of them to execute PowerShell script in C#, but what is the difference between them? 我们可以使用它们在C#中执行PowerShell脚本,但是它们之间有什么区别?

You should read the documentation. 您应该阅读文档。 A pipeline is a feature of a runspace . 一个pipeline是一个功能runspace The PowerShell.Create() method will create a PowerShell object, a kind of wrapper for everything contained. PowerShell.Create()方法将创建一个PowerShell对象,这是其中包含的所有内容的一种包装。 Both of these methods belong to the same PowerShell SDK. 这两种方法都属于同一个PowerShell SDK。

The Pipeline is intended to run commands and is underneath the runspace object. Pipeline用于运行命令,位于运行runspace对象的下方。

More 更多

Pipeline Class (msdn) 管道类(msdn)

PowerShell Class (msdn) PowerShell类(msdn)

Note: The PowerShell SDK documentation is very sparse, so the following is speculative . 注意: PowerShell SDK文档非常稀疏,因此以下内容是推测性的

  • An instance of the PowerShell class is a wrapper for a runspace , the container in which a PowerShell session runs; 的的一个实例PowerShell类为运行空间的包装,容器,其中一个PowerShell会话运行; its .RunSpace property returns the enclosed runspace. .RunSpace属性返回封闭的运行空间。

  • You need a runspace ( RunSpace instance) in order to create and execute a pipeline for executing arbitrary PowerShell statements. 您需要一个运行空间( RunSpace实例)才能创建和执行用于执行任意PowerShell语句的管道

  • To create a pipeline, you have two options: 要创建管道,您有两个选择:

    • If you have a PowerShell instance, you can use its convenience methods such as .AddScript() to implicitly create a pipeline. 如果您具有PowerShell实例,则可以使用其便捷方法(例如.AddScript() 隐式创建管道。

    • Alternatively, use a runspace's .CreatePipeline() method to create and manage a pipeline explicitly . 或者,使用运行空间的.CreatePipeline()方法显式创建和管理管道。

Simply put: The convenience methods of the PowerShell class allow simpler creation and execution of pipelines. 简而言之: PowerShell类的便捷方法允许更简单地创建和执行管道。

Note that both approaches support executing multiple statements, including any mix of commands (eg, cmdlet invocations) and expressions (eg, 1 + 2 ). 请注意, 这两种方法都支持执行多个语句,包括命令(例如,cmdlet调用)和表达式(例如1 + 2 )的任何混合。

The following snippet compares the two approaches (using PowerShell itself), which are functionally equivalent, from what I can tell: 下面的代码片段比较了两种方法(使用PowerShell本身),根据我的判断,它们在功能上是等效的:

# Create a PowerShell instance and use .AddScript() to implicitly create
# a pipeline that executes arbitrary statements.
[powershell]::Create().AddScript('Get-Date -DisplayHint Date').Invoke()

# The more verbose equivalent using the PowerShell instance's .RunSpace
# property and the RunSpace.CreatePipeline() method.
[powershell]::Create().RunSpace.CreatePipeline('Get-Date -DisplayHint Date').Invoke()

There may be subtleties that I'm missing; 我可能缺少一些微妙之处。 do tell us, if so. 如果是的话,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM