我试图从一个asp.net网页执行一个Powershell脚本,当它试图执行这个脚本时,我一直收到这个错误。我尝试过两种执行脚本的方法,两者都给出了相同的错误。完全错误是:
处理请求'C:\source\repos\WebApp\WebApp\Microsoft.PowerShell.Commands'.:无法加载PowerShell snap-in Microsoft.PowerShell.Diagnostics时发生未处理异常,原因是以下错误:无法加载文件或程序集PSSnapInException系统找不到指定的文件。
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();
}发布于 2019-10-28 19:05:36
我有一个类似的问题,并附带错误信息。在我的项目中安装用于Microsoft.PowerShell.SDK的Microsoft.PowerShell.SDK包就成功了。
https://stackoverflow.com/questions/57166406
复制相似问题