首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复错误“无法加载PowerShell snap-in Microsoft.PowerShell.Diagnostics,因为以下错误:无法加载文件或程序集”

如何修复错误“无法加载PowerShell snap-in Microsoft.PowerShell.Diagnostics,因为以下错误:无法加载文件或程序集”
EN

Stack Overflow用户
提问于 2019-07-23 14:27:41
回答 1查看 12.3K关注 0票数 18

我试图从一个asp.net网页执行一个Powershell脚本,当它试图执行这个脚本时,我一直收到这个错误。我尝试过两种执行脚本的方法,两者都给出了相同的错误。完全错误是:

处理请求'C:\source\repos\WebApp\WebApp\Microsoft.PowerShell.Commands'.:无法加载PowerShell snap-in Microsoft.PowerShell.Diagnostics时发生未处理异常,原因是以下错误:无法加载文件或程序集PSSnapInException系统找不到指定的文件。

代码语言:javascript
复制
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.
                }
            }
代码语言:javascript
复制
    using (PowerShell PowerShellInstance = PowerShell.Create())
                {   
  PowerShellInstance.AddScript("C:\\Users\\RSpotton\\Desktop\\test.ps1");
                    PowerShellInstance.Invoke();
                }
EN

回答 1

Stack Overflow用户

发布于 2019-10-28 19:05:36

我有一个类似的问题,并附带错误信息。在我的项目中安装用于Microsoft.PowerShell.SDKMicrosoft.PowerShell.SDK包就成功了。

票数 44
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57166406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档