我一直在尝试使用以下代码从一个.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,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'。系统找不到指定的文件。文件名:'Microsoft.Management.Infrastructure,Version=1.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35' 在 System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)、System.Reflection.RuntimeAssembly.GetExportedTypes()、System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)、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)、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)、System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)、System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()、System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)、System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()、System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)、System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)、System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection
1 input, PSDataCollection1 output, PSInvocationSettings settings)、System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)、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.Infrastructure (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,但问题依旧。
string command = @"& """c:\\my Folder\\myScript.ps1""";
。在这种情况下,您不需要转义反斜杠,请将代码更改为string command = @"& ""c:\my Folder\myScript.ps1"" ";
。 - Moerwald