我正在嘗試從 C# Visual Studio 解決方案呼叫 Powershell 檔案。
顯然,在除錯時,當它到達呼叫 PS1 檔案的行時,它似乎沒有做任何事情。
我收到這條訊息:

我在 C# 中有這些行:
using System.Management.Automation;
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"C:\Users\Justin\source\repos\HttpTrigger_1119\HttpTrigger_1119\list.ps1")).Invoke();
但是,當斷點碰到下一行時,并沒有顯示錯誤:

我的下一個嘗試是在 ps1 檔案本身中放置斷點。
但是,看起來它甚至沒有停在 ps1 檔案的斷點處。
因為,它沒有在 ps1 檔案內擊中斷點,所以呼叫 PS1 檔案可能會丟失一些東西,不是嗎?
從現有的兩行添加什么?
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"C:\Users\Justin\source\repos\HttpTrigger_1119\HttpTrigger_1119\list.ps1")).Invoke();
uj5u.com熱心網友回復:
關于“如何除錯...”的問題
重構你的代碼:
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"C:\Users\Justin\source\repos\HttpTrigger_1119\HttpTrigger_1119\list.ps1")).Invoke();
你可以在除錯器中觀察到的東西:
var ps1Script = File.ReadAllText(...);
var newPs = ps.AddScript(ps1Script);
var psResult = newPs.Invoke();
并逐步執行您的代碼以確保它按照您的預期運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/366195.html
