【发布时间】:2019-10-14 14:23:55
【问题描述】:
我一直在尝试使用以下代码从 .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(程序集 程序集、字符串名称、PSSnapInInfo psSnapInInfo、PSModuleInfo moduleInfo, Boolean isModuleLoad, Dictionary
2& cmdlets, Dictionary2& 别名,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& cmdlet,Dictionary2& aliases, Dictionary2& providers,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](PSDataCollection1 input, PSDataCollection1 输出,PSInvocationSettings 设置)在 System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 输出,PSInvocationSettings 设置)在 System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable 输入,PSDataCollection`1 输出,PSInvocationSettings 设置)在 System.Management.Automation.PowerShell.Invoke(IEnumerable 输入, 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) 但它并没有解决问题
编辑2 Dev 是 Windows 10 Pro,Prod 是 Windows Server 2016 Standard
编辑3 直接通过 PowerShell 启动时,该脚本在 prod 上运行良好。显示的错误是 Web 应用程序尝试运行 PowerShell。
编辑4 应用程序池帐户具有管理员权限,并且与我用于手动运行脚本的帐户相同(不是好的做法,但现在我只是尝试运行此脚本)。
编辑5 我尝试将dll放在服务器上应用程序的文件夹中并重新启动站点+回收应用程序池。我尝试了在开发机器上可以找到的每个版本,但没有任何效果:/
编辑6 将 nuget 包从 6.0.5 更新到最新的 6.2.1 ,同样的问题
【问题讨论】:
-
不幸的是我的代码是相同的,我有相同的 Nuget 包(但版本 6.0.5 而不是 6.0.0-rc)。实际上我从这个答案中找到了运行 powershell 的代码。
-
您正在使用
string command = @"& """c:\\my Folder\\myScript.ps1""";中的逐字标识符。在这种情况下,您不需要转义反斜杠,将代码更改为string command = @"& ""c:\my Folder\myScript.ps1"" "; -
你的开发环境是什么,生产环境是什么?
-
@DhruvMurarka Win 10 Pro for dev 和 Win Server 2016 Standard for prod.
标签: c# powershell asp.net-core .net-core