如何修复错误"Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following erro



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

处理

请求时发生未处理的异常 PSSnapInException: 無法載入 PowerShell 管理单元 Microsoft.PowerShell.Diagnostics 由于以下错误: 可能 不加载文件或程序集 'C:\source\repos\WebApp\WebApp\Microsoft.PowerShell.Command'.这 系统找不到指定的文件。

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();
                }

我遇到了类似的问题和随附的错误消息。将用于Microsoft.PowerShell.SDKNuGet 包安装到我的项目中可以解决问题。

最新更新