我想将 PowerShell 功能添加到 C# 程序中,并且我已将程序system.managment.automation.dll
集嵌入作为参考。我在 StackOverflow 上发现了两种从 C# 调用 PowerShell 脚本的方法:
使用运行空间:
不使用运行空间:
PowerShell powerShell = PowerShell.Create(); powerShell.AddScript("my script"); powerShell.Invoke();
根据这个网站,Powershell 实例似乎与其 C# 主机相关联。
Powershell 运行时作为运行空间调用,默认情况下,将同步处理命令并将处理结果传递给主机应用程序,以便它们可以返回给用户。
此外,PowerShell 脚本使用嵌入式system.managment.automation.dll
程序集而不是系统范围的程序集,因此该脚本将在逻辑上停止其 C# 主机。
用户关闭我的程序后,Powershell 脚本是否仍会运行?那两种方法呢?
PS:我想支持 PowerShell v2。