如何调试"调用"Powershell(ps1),从C#文件

0

的问题

我想援引Powershell文件从C#Visual Studio的解决方案。

显然,调试时,它的出现,它没有做任何事情的时命线调用PS1文件。

我得到这个消息:

enter image description here

我有这些行内部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();

然而,当破点打下一条线,但它并不显示错误: enter image description here

我的下一次尝试把断点内ps1文件本身。

但是,它的出现,它甚至没有停止在断点的ps1文件。

因为,它没有打断点内ps1文件,可能会有东西丢失援引PS1文件,不是吗?

任何东西添加从现有的两条线?

PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"C:\Users\Justin\source\repos\HttpTrigger_1119\HttpTrigger_1119\list.ps1")).Invoke();
c# powershell
2021-11-24 01:26:40
1

最好的答案

1

关于这个问题:"如何调试..."

重构代码:

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

和步骤,通过你的代码,以确保它在做什么你期待它。

2021-11-24 19:45:50

这正是我做了什么。 谢谢!
Java

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................