[英]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.SDK
的NuGet 包安装到我的项目中就成功了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.