简体   繁体   English

如何修复错误“由于以下错误无法加载 PowerShell 管理单元 Microsoft.PowerShell.Diagnostics:无法加载文件或程序集”

[英]How to fix error "Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly"

I'm trying to execute a Powershell script from an asp.net webpage and I keep getting this error when it tries to execute the script.我正在尝试从 asp.net 网页执行 Powershell 脚本,但在尝试执行该脚本时,我不断收到此错误。 I have tried 2 methods of executing the script and both give me the same error.我尝试了 2 种执行脚本的方法,但都给了我同样的错误。 The full error is:完整的错误是:

An unhandled exception occurred while processing the request PSSnapInException: Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly 'C:\\source\\repos\\WebApp\\WebApp\\Microsoft.PowerShell.Commands'.处理请求时出现未处理的异常 PSSnapInException:无法加载 PowerShell 管理单元 Microsoft.PowerShell.Diagnostics 由于以下错误:无法加载文件或程序集 'C:\\source\\repos\\WebApp\\WebApp\\Microsoft.PowerShell.Commands '。 The system cannot find the file specified.该系统找不到指定的文件。

PowerShell powershell = PowerShell.Create();
            using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                runspace.Open();
                powershell.Runspace = runspace;
                System.IO.StreamReader sr = new System.IO.StreamReader("C:\\Desktop\\test.ps1");
                powershell.AddScript(sr.ReadToEnd());
                var results = powershell.Invoke();
                if (powershell.Streams.Error.Count > 0)
                {
                    // error records were written to the error stream.
                    // do something with the items found.
                }
            }
    using (PowerShell PowerShellInstance = PowerShell.Create())
                {   
  PowerShellInstance.AddScript("C:\\Users\\RSpotton\\Desktop\\test.ps1");
                    PowerShellInstance.Invoke();
                }

I had a similar issue and accompanying error message.我有一个类似的问题和伴随的错误消息。 Installing the NuGet package for Microsoft.PowerShell.SDK into my project did the trick.Microsoft.PowerShell.SDKNuGet 包安装到我的项目中就成功了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复此错误“Microsoft.Extensions.Azure:无法加载文件或程序集'System.Diagnostics.Tracing,版本= 5.0.0.0” - how to fix this error "Microsoft.Extensions.Azure: Could not load file or assembly 'System.Diagnostics.Tracing, Version=5.0.0.0," 如何解决以下错误“无法加载文件或程序集'microsoft aspnetcore razor runtime 3.1 1” - How do you solve the following error “could not load file or assembly 'microsoft aspnetcore razor runtime 3.1 1” 如何修复 Visual Studio 2015 中的错误“无法加载文件或程序集 'Microsoft.VisualStudio.Web”? - How to fix error “Could not load file or assembly 'Microsoft.VisualStudio.Web” in Visual Studio 2015? 无法加载文件或程序集“Microsoft.Diagnostics.Tracing.EventSource - Could not load file or assembly 'Microsoft.Diagnostics.Tracing.EventSource 如何修复“无法加载文件或程序集System.Core”错误? - How to fix “Could not load file or assembly System.Core” error? 错误:无法在 SSIS 中加载文件或程序集“Microsoft.WindowsAzure.Configuration” - Error: Could not load file or assembly 'Microsoft.WindowsAzure.Configuration in SSIS 无法在应用程序中加载文件或Microsoft.mshtml程序集错误 - Could not load file or assembly of Microsoft.mshtml error in application 无法加载文件或程序集错误 - Could not load file or assembly error 服务器错误/应用程序出现以下错误消息:更改为 MVC5 后无法加载文件或程序集“Microsoft.Web.Infrastructure” - Server error / application with the following error message: Could not load file or assembly 'Microsoft.Web.Infrastructure after changing to MVC5 PowerShell 7.0.x 无法加载文件或程序集 Microsoft.CodeAnalysis - PowerShell 7.0.x Could not load file or assembly Microsoft.CodeAnalysis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM