C# 是否有System.Management.Automation.RunspaceInvoke的.NET核心替代方案?
我目前正在将一个库从.NETFramework移植到.NETCore。该库的框架版本使用了Powershell中的一些运行空间,我对此不太熟悉(为了回答这个问题,撇开在Core中使用Powershell的优点不谈)。有关守则如下:C# 是否有System.Management.Automation.RunspaceInvoke的.NET核心替代方案?,c#,powershell,.net-core,automation,runspace,C#,Powershell,.net Core,Automation,Runspace,我目前正在将一个库从.NETFramework移植到.NETCore。该库的框架版本使用了Powershell中的一些运行空间,我对此不太熟悉(为了回答这个问题,撇开在Core中使用Powershell的优点不谈)。有关守则如下: using System.Management.Automation; using System.Management.Automation.Runspaces; ... RunspaceConfiguration runspaceConfig
using System.Management.Automation;
using System.Management.Automation.Runspaces;
...
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
using (RunspaceInvoke invoker = new RunspaceInvoke(runspace))
{
output = invoker.Invoke("Set-ExecutionPolicy Unrestricted");
PrintOutput(output);
...
我知道,在.NETCore中,RunspaceConfiguration已经被弃用,转而使用InitialSessionState。但我不确定如何处理RunspaceInvoke,据我所知,.NETCore中不存在RunspaceInvoke。在.NET Core中调用运行空间(或执行一些适当的变通方法)需要使用什么?运行空间只是Powershell的一个实例 要在.NET Core中创建PowerShell实例,请使用System.Management.Automation的PowerShell类及其PowerShell.create()方法:
在我看来,它是受支持的,因为它是
System.Management.Automation
NameSpace/Assembly的一部分dotnet add package System.Management.Automation--version 7.0.2
我对.net core
了解不多,因此如果我完全不正确,我深表歉意,但是根据文档,应该支持RunspaceInvoke
。System.Management.Automation在.net core中受支持。但是,RunspaceInvoke需要安装Microsoft.PowerShell.5.ReferenceAssembly,它仅在.NET Framework上运行。在Visual Studio中,安装此软件包时会发出“此软件包可能与您的项目不完全兼容”警告。谢谢Jacob,我在家时会详细研究此问题,看看是否有Microsoft.PowerShell.5.ReferenceAssembly的替代方案。您是否尝试使用包加载您的项目,即使它发出编译警告,但它不起作用?PowerShell.Create()方法的表示方法暗示了上述标识(runspace=PowerShell实例),但它不是显式的。在采用InitialSessionState参数的方法版本下,它声明:=====================“参数InitialSessionState用于创建运行空间,并返回PowerShell的PowerShell实例================
InitialSessionState runspaceConfiguration = InitialSessionState.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
using (PowerShell invoker = PowerShell.Create(runspace))
{
output = invoker.Invoke("Set-ExecutionPolicy Unrestricted");
PrintOutput(output);