Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “如何修复错误”;由于以下错误,无法加载Microsoft.PowerShell.Diagnostics中的PowerShell管理单元:无法加载文件或程序集;_C#_Asp.net_Powershell - Fatal编程技术网

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

C# “如何修复错误”;由于以下错误,无法加载Microsoft.PowerShell.Diagnostics中的PowerShell管理单元:无法加载文件或程序集;,c#,asp.net,powershell,C#,Asp.net,Powershell,我正在尝试从asp.net网页执行Powershell脚本,当它尝试执行脚本时,我不断收到此错误。我尝试了两种执行脚本的方法,两种方法都给出了相同的错误。完全错误是: 处理请求时发生未处理的异常 PSSnapInException:无法加载PowerShell管理单元 由于以下错误,Microsoft.PowerShell.Diagnostics无法运行: 不加载文件或程序集 'C:\source\repos\WebApp\WebApp\Microsoft.PowerShell.Commands

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

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


我有一个类似的问题和附带的错误消息。将Microsoft.PowerShell.SDK的NuGet软件包安装到我的项目中成功了。

如果能更好地记录在C#中使用PowerShell,那就太好了。谢谢BinaryBurnie。如果它对任何人都有帮助,那么在github的一个问题中也发现了这一点:谢谢
dotnet add Microsoft.PowerShell.SDK
在我的例子中,我不得不卸载这个软件包并安装一个旧版本(从7.1版升级到6.2.7版)。其他相关软件包,如System.Management.Autoamtion,也是6.2.7
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();
                }