从.Net Core运行Powershell-无法加载文件或程序集Microsoft.Management.Infrastructure

Syl*_*ois 10 c# powershell .net-core asp.net-core

我一直在尝试使用以下代码从.Net Core Web应用程序运行Powershell脚本(此处不讨论最佳实践;))。

    string command = @"& """c:\\my Folder\\myScript.ps1""";

    using (var ps = PowerShell.Create())
    {
        var results = ps.AddScript(command).Invoke();
    }
Run Code Online (Sandbox Code Playgroud)

它在我的开发机上运行良好,但是在生产中尝试执行此功能时失败:

ps.AddScript(command).Invoke()
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.Management.Infrastructure,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'。该系统找不到指定的文件。文件名:System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly程序集,ObjectHandleOnStack retTypes)在System.Reflection.RuntimeAssembly.GetExportedTypes()处的Microsoft.Management.Infrastructure,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35 System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly,String name,PSSnapInInfo psSnapInInfo,PSModuleInfo moduleInfo,Boolean is.System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly,String name)的System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(组装程序集,字符串名称)2& cmdlets, Dictionary2&别名,System 2.System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo,PSSnapInException&warning)的System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()处的别名2& providers, String helpFile, Type& randomCmdletToCheckLinkDemand, Type& randomProviderToCheckLinkDemand) at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Boolean isModuleLoad, Dictionary2,cmdlet,字典2& aliases, Dictionary2&提供程序,String&helpFile)。系统。管理.Automation.PowerShell.CoreInvokeHelper [TInput,TOutput](PSDataCollection1 input, PSDataCollectionSystem.Management.Automation.PowerShell.CoreInvoke [TInput,TOutput](PSDataCollection 1 input, PSDataCollection1输出,PSInvocationSettings设置)(System.Management.Automation.PowerShell.CoreInvoke [TOutput](IEnumerable输入,PSDataCollection`1输出)上的输出,PSInvocationSettings设置)位于System.Management.Automation.PowerShell.Invoke(IEnumerable输入,PSInvocationSettings设置)

我不知道该安装哪个框架/程序包以使其运行。目标框架是已安装的.Net Core 2.1,因此,除了上述内容外,该应用程序运行正常。

deps.json文件包含以下内容:

    "Microsoft.Management.Infrastructure/1.0.0": {
        "dependencies": {
          "NETStandard.Library": "2.0.3",
          "System.Runtime.CompilerServices.VisualC": "4.3.0",
          "System.Runtime.Serialization.Xml": "4.3.0",
          "System.Security.SecureString": "4.3.0",
          "System.Threading.ThreadPool": "4.3.0"
        },
        "compile": {
          "ref/netstandard1.6/Microsoft.Management.Infrastructure.Native.dll": {},
          "ref/netstandard1.6/Microsoft.Management.Infrastructure.dll": {}
        }
      }
Run Code Online (Sandbox Code Playgroud)

安装的Nuget软件包:

Microsoft.AspNetCore.App (2.1.1)
Microsoft.PowerShell.Commands.Diagnostics (6.0.5)
Microsoft.PowerShell.SDK (6.0.5)
Microsoft.PowerShell.WSMan.Management (6.0.5)
Run Code Online (Sandbox Code Playgroud)

编辑 我还添加了Microsoft.Management.Infrastructure(1.0.0),但没有解决问题

Edit2 Dev是Windows 10 Pro,而Prod是Windows Server 2016 Standard

Edit3 直接通过PowerShell启动时,脚本可以在prod上正常运行。显示的错误是Web应用程序尝试运行PowerShell时的错误。

Edit4 应用程序池帐户具有管理员权限,与我用来手动运行脚本的帐户相同(虽然不是很好的做法,但现在我只是尝试运行此脚本)。

Edit5 我试图将dll放在服务器上应用程序的文件夹中,然后重新启动站点+回收应用程序池。我尝试了在开发机器上可以找到的每个版本,但没有任何效果:/

Edit6将 nuget软件包从6.0.5更新到了最新的6.2.1,同样的问题

Geo*_*dze 17

我有同样的问题,Microsoft.Management.Infrastructure.dll(和相关文件)没有发布。通过指定一个解决非便携式RID中发布配置(*.pubxml所述*.csproj):

<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
Run Code Online (Sandbox Code Playgroud)

问题可能是由以下事实引起的,即C:\Users\UserName\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes只有具有不可移植 RID 的文件夹;没有win-x86win-x64