我一直在尝试使用以下代码从 .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: 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, Dictionary
2& cmdlets, Dictionary
2& 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, Dictionary
2& cmdlets, Dictionary2& aliases, Dictionary
2& 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, PSDataCollection
1 output, PSInvocationSettings settings) at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection
1 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,因此除了上面提到的行之外,应用程序运行良好。
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.Infrastruct (1.0.0) 但这并没有解决问题
编辑2 Dev 是 Windows 10 Pro,Prod 是 Windows Server 2016 Standard
编辑3 当直接通过 PowerShell 启动时,该脚本在产品上运行良好。当 Web 应用尝试运行 PowerShell 时显示错误。
编辑4 应用程序池帐户具有管理员权限,并且与我用于手动运行脚本的帐户相同(不是很好的做法,但现在我只是尝试运行此脚本)。
编辑5 我尝试将dll放在服务器上应用程序的文件夹中,然后重新启动站点+回收应用程序池。我尝试了在开发机器上可以找到的每个版本,但没有任何效果:/
编辑6 将 nuget 包从 6.0.5 更新到最新的 6.2.1,同样的问题
最佳答案
我遇到了同样的问题,Microsoft.Management.Infrastruct.dll
(及相关文件)未发布。
通过指定 non-portable RID 来解决在发布配置文件中(*.pubxml
,不是 *.csproj
):
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
问题可能是由于以下事实引起的:C:\Users\UserName\.nuget\packages\microsoft.management.infrastruct\1.0.0\runtimes
下只有包含非可移植 RID;没有 win-x86
和 win-x64
。
关于c# - 从 .Net Core 运行 Powershell - 无法加载文件或程序集 Microsoft.Management.Infrastruct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56352672/