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

查看:157
本文介绍了从.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,Version = 1.0.0.0,
文化=中立,公共K eyToken = 31bf3856ad364e35',位于
System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly
组件,ObjectHandleOnStack retTypes)位于
System.Reflection.RuntimeAssembly.GetExportedTypes(),位于
System.Management。 Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(程序集
程序集,字符串名称)位于
System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(程序集
程序集,字符串名称,PSSnapInInfo psSnapInInfo,PSModuleInfo b $ b moduleInfo,布尔值isModuleLoad,字典 2& cmdlet,字典 2&
别名,Dictionary 2&提供程序,字符串helpFile,Type&
randomCmdletToCheckLinkDemand,Type& randomProviderToCheckLinkDemand)
at
System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly
程序集,字符串名称,PSSnapInInfo psSnapInInfo,PSModuleInfo
moduleInfo,布尔值isModuleLoad,字典
2& cmdlet,字典 2&
别名,字典
2&提供程序,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(Runspace
rsToUse,Boolean isSync)在
处System.Management.Automation.PowerShel l.CoreInvokeHelper [TInput,TOutput](PSDataCollection 1
输入,PSDataCollection
1输出,PSInvocationSettings设置)位于
System.Management.Automation.PowerShell。 CoreInvoke [TInput,TOutput](PSDataCollection 1
输入,PSDataCollection
1输出,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)

我不知道我应该安装哪个框架/软件包以使其运行。
Target Framework是安装的.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

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

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

Edit3
直接通过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.

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

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).

Edit5
我试图将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 :/

Edit6
从6.0更新了nuget软件包。 5至最新的6.2.1,相同的问题

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

推荐答案

我遇到了相同的问题, Microsoft.Management.Infrastructure .dll (及相关文件)未发布。
通过指定非便携式RID 在发布配置文件中( *。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下:\用户\UserName\.nuget\软件包\microsoft.management.infrastructure\1.0.0\运行时只有具有不可移植RID的文件夹。没有 win-x86 win-x64

Problem is probably caused by the fact, that under C:\Users\UserName\.nuget\packages\microsoft.management.infrastructure\1.0.0\runtimes 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天全站免登陆