簡體   English   中英

如何修復錯誤“由於以下錯誤無法加載 PowerShell 管理單元 Microsoft.PowerShell.Diagnostics:無法加載文件或程序集”

[英]How to fix error "Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly"

我正在嘗試從 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.
                }
            }
    using (PowerShell PowerShellInstance = PowerShell.Create())
                {   
  PowerShellInstance.AddScript("C:\\Users\\RSpotton\\Desktop\\test.ps1");
                    PowerShellInstance.Invoke();
                }

我有一個類似的問題和伴隨的錯誤消息。 Microsoft.PowerShell.SDKNuGet 包安裝到我的項目中就成功了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM