小编Ril*_*ton的帖子

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

我正在尝试从 asp.net 网页执行 Powershell 脚本,但在尝试执行该脚本时,我不断收到此错误。我尝试了 2 种执行脚本的方法,但都给了我同样的错误。完整的错误是:

处理请求时出现未处理的异常 PSSnapInException:无法加载 PowerShell 管理单元 Microsoft.PowerShell.Diagnostics 由于以下错误:无法加载文件或程序集 'C:\source\repos\WebApp\WebApp\Microsoft.PowerShell.Commands '。该系统找不到指定的文件。

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.
                }
            }
Run Code Online (Sandbox Code Playgroud)
    using (PowerShell PowerShellInstance = PowerShell.Create())
                {   
  PowerShellInstance.AddScript("C:\\Users\\RSpotton\\Desktop\\test.ps1");
                    PowerShellInstance.Invoke();
                }
Run Code Online (Sandbox Code Playgroud)

c# asp.net powershell

12
推荐指数
1
解决办法
5475
查看次数

标签 统计

asp.net ×1

c# ×1

powershell ×1