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

时间:2019-05-29 03:23:06

标签: c# powershell asp.net-core .net-core

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

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

    using (var ps = PowerShell.Create())
    {
        var results = ps.AddScript(command).Invoke();
    }

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

ps.AddScript(command).Invoke()

我收到以下异常:

  

System.IO.FileNotFoundException:无法加载文件或程序集   'Microsoft.Management.Infrastructure,版本= 1.0.0.0,   文化=中性,PublicKeyToken = 31bf3856ad364e35'。系统无法   查找指定的文件。文件名:   'Microsoft.Management.Infrastructure,版本= 1.0.0.0,   文化=中性,PublicKeyToken = 31bf3856ad364e35'在   System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly   程序集,ObjectHandleOnStack retTypes)位于   System.Reflection.RuntimeAssembly.GetExportedTypes()在   System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(装配体   程序集,字符串名称)在   System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly   程序集,字符串名称,PSSnapInInfo psSnapInInfo,PSModuleInfo   moduleInfo,布尔值isModuleLoad,字典2& cmdlets, Dictionary 2&   别名,Dictionary 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, Dictionary 2&cmdlet,Dictionary 2& aliases, Dictionary 2&provider,String&helpFile)   System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo   psSnapInInfo,PSSnapInException&警告),网址为   System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()   在   System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost   主持人)   System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()   在   System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(运行空间   rsToUse,布尔值isSync)位于   System.Management.Automation.PowerShell.CoreInvokeHelper [TInput,TOutput](PSDataCollection 1 input, PSDataCollection 1输出,PSInvocationSettings设置)位于   System.Management.Automation.PowerShell.CoreInvoke [TInput,TOutput](PSDataCollection 1 input, PSDataCollection 1输出,PSInvocationSettings设置)位于   System.Management.Automation.PowerShell.CoreInvoke [TOutput](IEnumerable   输入,PSDataCollection`1输出,PSInvocationSettings设置)   System.Management.Automation.PowerShell.Invoke(即可输入的数字,   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": {}
        }
      }

已安装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)

修改     我还添加了Microsoft.Management.Infrastructure(1.0.0) 但这并不能解决问题

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

3 个答案:

答案 0 :(得分:2)

在池中检​​查Web应用程序的应用程序标识池。 可能是供股 使用身份模拟或在管理员帐户上运行。 当您从控制台运行时,您将以自己的身份运行,而在应用程序及其应用程序身份池中运行

答案 1 :(得分:2)

我遇到了同样的问题,Microsoft.Management.Infrastructure.dll(及相关文件)未发布。 通过在发布配置文件中指定non-portable RID*.pubxml不是 *.csproj)来解决:

<RuntimeIdentifier>win7-x64</RuntimeIdentifier>

问题可能是由于以下事实造成的:C:\Users\UserName\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes下仅存在具有不可移植RID的文件夹;没有win-x86win-x64

答案 2 :(得分:0)

您可以从Powershell加载任何DLL,因此绝对可行的一种解决方案是加载Microsoft.Management.Infrastructure DLL作为脚本的一部分。但是在此之前,让我们验证一下在dev和prod powershell会话中加载的内容之间的区别。

您可以通过运行[Threading.Thread]::GetDomain().GetAssemblies()来获取PowerShell中当前加载的程序集的列表。在脚本的开头和结尾添加此行(在结尾处添加,因为在脚本中首次使用它们时,PowerShell将自动加载引用的程序集)。现在,在dev和prod上运行脚本并比较结果。很有可能Microsoft.Management.Infrastructure在产品上会丢失。

程序集列表输出显示了程序集的位置。从开发人员列表中,获取Microsoft.Management.Infrastructure DLL文件。现在,您有两个选择:

  1. 将文件放入生产机的GAC中。 PowerShell现在应根据需要自动加载DLL。
  2. 使此DLL文件的加载成为PowerShell脚本的一部分。在脚本的开头(或使用[Reflection.Assembly]::LoadFile($fullPathToDLL)之前的任何位置)添加Microsoft.Management.Infrastructure
相关问题