C# .NET核心项目中的System.Automation.Runspaces错误
我试着在控制台应用程序中运行一段代码-C# .NET核心项目中的System.Automation.Runspaces错误,c#,asp.net,asp.net-mvc,powershell,C#,Asp.net,Asp.net Mvc,Powershell,我试着在控制台应用程序中运行一段代码-C(.netframework4.5项目),它运行正常。但是当我尝试将其迁移到ASP.NET Core 2.0时,代码将返回一个错误(如下所示) 错误是 “'System.PlatformNotSupportedException:'ReflectionOnly loading' 在此平台上不受支持。“” 有什么想法吗?使用Powershell.Create()而不是管道,在继续之前调用导入模块,使用添加命令而不是添加脚本 尝试:(打开运行空间后,例如run
C
(.netframework4.5
项目),它运行正常。但是当我尝试将其迁移到ASP.NET Core 2.0时,代码将返回一个错误(如下所示)
错误是
“'System.PlatformNotSupportedException:'ReflectionOnly loading'
在此平台上不受支持。“”
有什么想法吗?使用
Powershell.Create()
而不是管道,在继续之前调用导入模块
,使用添加命令
而不是添加脚本
尝试:(打开运行空间后,例如runspace.Open();
)
可能重复的
using System.Management.Automation.Runspaces;
public void Powershell()
{
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Import-Module AzureAD -Force;");
pipeline.Commands.AddScript("$password = ConvertTo-SecureString " + "\"Accenture01\"" + " -AsPlainText -Force");
pipeline.Commands.AddScript("$Cred = New-Object System.Management.Automation.PSCredential (" + "\"TestID01@eso.dev.accenture.com\"" + ", $password)");
pipeline.Commands.AddScript("Connect-AzureAD -Credential $Cred");
pipeline.Commands.AddScript("Get-AzureADApplication -Filter " + "\"DisplayName eq " + "\'TestApp\'" + "\"");
var result = pipeline.Invoke();
}
using (var powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
powershell.Commands.AddCommand("Import-Module").AddArgument("AzureAD");
powershell.Invoke();
powershell.Commands.Clear();
powershell.AddScript("$password = ConvertTo-SecureString " + "\"Accenture01\"" + " -AsPlainText -Force");
powershell.AddScript("$Cred = New-Object System.Management.Automation.PSCredential (" + "\"TestID01@eso.dev.accenture.com\"" + ", $password)");
powershell.AddScript("Connect-AzureAD -Credential $Cred");
powershell.AddScript("Get-AzureADApplication -Filter " + "\"DisplayName eq " + "\'TestApp\'" + "\"");
powershell.Invoke();
var results = powershell.Invoke();
}