[英]Difference Between Pipeline and PowerShell class in C#
我想知道使用Pipeline
類與PowerShell
類在C#
執行PowerShell腳本之間的區別。
使用管道:
Pipeline pipe = runspace.CreatePipeline();
使用PowerShell類:
PowerShell ps = PowerShell.Create();
我們可以使用它們在C#中執行PowerShell腳本,但是它們之間有什么區別?
您應該閱讀文檔。 一個pipeline
是一個功能runspace
。 PowerShell.Create()
方法將創建一個PowerShell
對象,這是其中包含的所有內容的一種包裝。 這兩種方法都屬於同一個PowerShell SDK。
Pipeline
用於運行命令,位於運行runspace
對象的下方。
注意: PowerShell SDK文檔非常稀疏,因此以下內容是推測性的 。
的的一個實例PowerShell
類為運行空間的包裝,容器,其中一個PowerShell會話運行; 其.RunSpace
屬性返回封閉的運行空間。
您需要一個運行空間( RunSpace
實例)才能創建和執行用於執行任意PowerShell語句的管道 。
要創建管道,您有兩個選擇:
如果您具有PowerShell
實例,則可以使用其便捷方法(例如.AddScript()
隱式創建管道。
或者,使用運行空間的.CreatePipeline()
方法顯式創建和管理管道。
簡而言之: PowerShell
類的便捷方法允許更簡單地創建和執行管道。
請注意, 這兩種方法都支持執行多個語句,包括命令(例如,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()
我可能缺少一些微妙之處。 如果是的話,請告訴我們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.