为什么在 C# 中的 Powershell 脚本中运行空间

Why Runspace in Powershell scripts in C#

我想澄清一件事,即为什么我们使用 Runspace 而 运行 在 C# 中使用 PowerShell 脚本。它 运行s 即使没有 Runspace.

如果没有 Runspace 就可以 运行 为什么我们需要?

例如:

PowerShell ps = PowerShell.Create();
ps.AddScript("Import-Module AzureAD");

即使 Runspace 不存在,这仍然有效。那为什么会这样?

Runspace 提供用于创建管道、访问会话状态等的 API。

使用PowerShell时,我们需要使用System.Management.Automation.PowerShell,它正在使用System.Management.Automation.Runspaces

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;

namespace System.Management.Automation
{
    public sealed class PowerShell : IDisposable
    {
        ......
    }
}