C# 中管道和 PowerShell class 的区别
Difference Between Pipeline and PowerShell class in C#
我想知道在 C#
中使用 Pipeline
class 与 PowerShell
class.[=16= 执行 PowerShell 脚本之间的区别]
使用管道:
Pipeline pipe = runspace.CreatePipeline();
使用 PowerShell class:
PowerShell ps = PowerShell.Create();
我们可以使用它们来在C#中执行PowerShell脚本,但它们有什么区别?
您应该阅读文档。 pipeline
是 runspace
的特征。 PowerShell.Create()
方法将创建一个 PowerShell
对象,一种包含所有内容的包装器。这两种方法属于同一个PowerShell SDK。
Pipeline
用于 运行 命令,位于 runspace
对象下方。
更多
注:PowerShell SDK documentation非常稀疏,所以以下是推测。
PowerShell
class 的一个实例是 runspace 的包装器,PowerShell 会话在其中运行;它的 .RunSpace
属性 returns 封闭的运行空间。
您需要一个运行空间(RunSpace
实例)来创建和执行 管道 以执行任意 PowerShell 语句。
要创建管道,您有两个选择:
如果您有一个 PowerShell
实例,您可以使用它的便捷方法,例如 .AddScript()
来 隐式地 创建一个管道。
或者,使用运行空间的 .CreatePipeline()
方法来创建和管理管道显式。
简单地说:PowerShell
class 的便捷方法允许更简单地创建和执行管道。
请注意,两种方法都支持执行多个语句,包括命令(例如cmdlet调用)和表达式(例如, 1 + 2
).
以下代码段比较了两种方法(使用 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()
我可能遗漏了一些细微之处;如果是,请告诉我们。
我想知道在 C#
中使用 Pipeline
class 与 PowerShell
class.[=16= 执行 PowerShell 脚本之间的区别]
使用管道:
Pipeline pipe = runspace.CreatePipeline();
使用 PowerShell class:
PowerShell ps = PowerShell.Create();
我们可以使用它们来在C#中执行PowerShell脚本,但它们有什么区别?
您应该阅读文档。 pipeline
是 runspace
的特征。 PowerShell.Create()
方法将创建一个 PowerShell
对象,一种包含所有内容的包装器。这两种方法属于同一个PowerShell SDK。
Pipeline
用于 运行 命令,位于 runspace
对象下方。
更多
注:PowerShell SDK documentation非常稀疏,所以以下是推测。
PowerShell
class 的一个实例是 runspace 的包装器,PowerShell 会话在其中运行;它的.RunSpace
属性 returns 封闭的运行空间。您需要一个运行空间(
RunSpace
实例)来创建和执行 管道 以执行任意 PowerShell 语句。要创建管道,您有两个选择:
如果您有一个
PowerShell
实例,您可以使用它的便捷方法,例如.AddScript()
来 隐式地 创建一个管道。或者,使用运行空间的
.CreatePipeline()
方法来创建和管理管道显式。
简单地说:PowerShell
class 的便捷方法允许更简单地创建和执行管道。
请注意,两种方法都支持执行多个语句,包括命令(例如cmdlet调用)和表达式(例如, 1 + 2
).
以下代码段比较了两种方法(使用 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()
我可能遗漏了一些细微之处;如果是,请告诉我们。