从 .Net Core 运行 Powershell - 无法加载文件或程序集 Microsoft.Management.Infrastructure [英] Running Powershell from .Net Core - Could not load file or assembly Microsoft.Management.Infrastructure

查看:24
本文介绍了从 .Net Core 运行 Powershell - 无法加载文件或程序集 Microsoft.Management.Infrastructure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to run a powershell script from a .Net Core Web app (not discussing best practices here ;) ) with the following code:

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

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

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

It works well on my dev machine, but in production it fails when trying to execute this function:

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' atSystem.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly程序集,ObjectHandleOnStack retTypes)在System.Reflection.RuntimeAssembly.GetExportedTypes() 在System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly程序集,字符串名称)在System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly程序集、字符串名称、PSSnapInInfo psSnapInInfo、PSModuleInfomoduleInfo, Boolean isModuleLoad, Dictionary2&cmdlet,字典2&别名,Dictionary2&提供者,字符串帮助文件,类型&randomCmdletToCheckLinkDemand, Type&randomProviderToCheckLinkDemand)在System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly程序集、字符串名称、PSSnapInInfo psSnapInInfo、PSModuleInfomoduleInfo, Boolean isModuleLoad, Dictionary2&cmdlet,Dictionary2&别名,字典2&提供者,String&帮助文件)在System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfopsSnapInInfo、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(RunspacersToUse,布尔值 isSync) 在System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1输入,PSDataCollection1 输出,PSInvocationSettings 设置)在System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1输入,PSDataCollection1 输出,PSInvocationSettings 设置)在System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable输入,PSDataCollection`1 输出,PSInvocationSettings 设置)在System.Management.Automation.PowerShell.Invoke(IEnumerable 输入,PSInvocationSettings 设置)

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes) at System.Reflection.RuntimeAssembly.GetExportedTypes() at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name) at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Boolean isModuleLoad, Dictionary2& cmdlets, Dictionary2& aliases, Dictionary2& 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& cmdlets, Dictionary2& aliases, Dictionary2& providers, String& helpFile) at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning) at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault() at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host) at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync) at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)

我不知道要安装哪个框架/包才能使其运行.目标框架是已安装的 .Net Core 2.1,因此应用程序运行良好,除了上面提到的行.

I don't know which framework/package I am suppose to install to make it run. Target Framework is .Net Core 2.1 which is installed, hence the application running fine except line mentionned above.

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

The deps.json file contains this:

    "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 软件包:

Nuget Packages installed:

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)但它没有解决问题

Edit I also added Microsoft.Management.Infrastructure (1.0.0) but it didn't fix the issue

编辑 2Dev 是 Windows 10 Pro,Prod 是 Windows Server 2016 Standard

Edit2 Dev is Windows 10 Pro and Prod is Windows Server 2016 Standard

编辑 3当直接通过 PowerShell 启动时,脚本在 prod 上运行良好.显示的错误是 Web 应用程序尝试运行 PowerShell 时的错误.

Edit3 The script runs fine on prod when launched through PowerShell directly. The error shown is when the Web app tries to run PowerShell.

编辑4应用程序池帐户具有管理员权限,并且与我用来手动运行脚本的帐户相同(不是好的做法,但现在我只是想使其运行).

Edit4 App pool account has admin rights and is the same account that I used to run the script manually (not good practice but for now I'm just trying to make this run).

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

Edit5 I tried to put the dll in the folder of the app on the server and restart the site + recycle the application pool. I tried with every version I could find on the dev machine and nothing worked :/

编辑6将 nuget 包从 6.0.5 更新到最新的 6.2.1 ,同样的问题

Edit6 Updated nuget packages from 6.0.5 to latest 6.2.1 , same issue

推荐答案

我遇到了同样的问题,Microsoft.Management.Infrastructure.dll(和相关文件)没有发布.通过在发布配置文件(*.pubxml不是*.csproj):

I had same issue, Microsoft.Management.Infrastructure.dll (and associated files) did not get published. Solved by specifying a non-portable RID in publish profile (*.pubxml, not the *.csproj):

<RuntimeIdentifier>win7-x64</RuntimeIdentifier>

问题可能是由于在C:UsersUserName.nugetpackagesmicrosoft.management.infrastructure1.0.0 untimes 下只有非便携式 RID;没有 win-x86win-x64.

Problem is probably caused by the fact, that under C:UsersUserName.nugetpackagesmicrosoft.management.infrastructure1.0.0 untimes there are only folders with non-portable RIDs; there is no win-x86 and win-x64.

这篇关于从 .Net Core 运行 Powershell - 无法加载文件或程序集 Microsoft.Management.Infrastructure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆