簡體   English   中英

我可以使用.Net標准庫中的PowerShell類在.Net Framework項目(exe)中使用嗎

[英]Can I use PowerShell class in .Net Standard library to use in .Net Framework project (exe)

我正在嘗試創建一個.net標准2.0庫,該庫使用PowerShell類(System.Management.Automation)調用PowerShell命令和腳本。 我想在.Net Framework(4.7.2)項目中使用此.Net標准庫。

我已經嘗試在.Net Standard項目中使用Microsoft.PowerShell.SDKSystem.Management.Automation NuGet程序包,但我一直收到此錯誤:

System.IO.FileLoadException

 HResult=0x80131040
 Message=Could not load file or assembly'System.Management.Automation, Version=6.1.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

.Net Standard 2.0庫項目中的示例代碼:

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

PowerShell posh = PowerShell.Create();
posh.Runspace = runspace;

我該如何實現? 任何幫助表示贊賞。

如果要定位.NET Standard,則應引用PowerShellStandard.Library nuget包

PowerShell Standard僅公開在PowerShell 5.1(.NET Framework)和PowerShell Core(.NET Core)之間重疊的API表面,因此無論您針對哪個版本進行編譯,它都可以使用。

推薦閱讀: PowerShell標准庫:構建可在Windows PowerShell和PowerShell Core上運行的單個模塊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM