使用 system.managment.automation 的 Powershell 脚本在其 C# 主机死后仍会 运行 吗?

Does a Powershell script using system.managment.automation would still run after its C# host die?

我想向 C# 程序添加 PowerShell 功能,并且 我嵌入了 system.managment.automation.dll 程序集作为参考。 我在 Whosebug 上找到了两种从 C# 调用 PowerShell scrit 的方法:

根据 this website,Powershell 实例似乎绑定到它的 C# 主机。

The Powershell runtime is invoked as a runspace and will, by default, process commands synchronously and deliver the results of processing to the host application so that they can be returned to the user.

此外,PowerShell 脚本使用嵌入式 system.managment.automation.dll 程序集而不是系统范围的程序集,因此该脚本在逻辑上会在其 C# 主机上停止。

Powershell 脚本在用户关闭我的程序后是否仍然运行?这两种方法如何?

PS: 我想支持 PowerShell v2.

让我们倒过来看一下:

What about the two methods ?

没有 运行宁 powershell 代码这样的东西,“not 使用 运行spaces”。

简单的解释就是运行space是核心先决条件——顾名思义,运行space是space 其中powershell代码运行s.

当你这样做时:

PowerShell powerShell = PowerShell.Create()

a 运行space 隐式创建并附加到 PowerShell 实例。

唯一的区别是您只需将对 运行space 创建的控制权交给 System.Management.Automation API,而不是自己实例化它。

Does the Powershell script would still run after the user close my program ?

主机应用程序终止后脚本会继续执行的唯一情况是当您连接到远程运行 space,即。 运行space 托管在另一个进程或另一台计算机上。

否则,运行space(隐式或非隐式)将在主机应用程序退出时与其余部分一起处理。