如何为同一台机器自动化 PowerShell 或 PowerShell Core

How to automate either PowerShell or PowerShell Core for same machine

随着 PowerShell Core 的发布,应用程序在使用托管自动化库 (system.management.automation) 时如何选择调用哪个版本的 Powershell(Powershell 5.x 或 PowerShell Core)?关于应该创建的运行空间?或者连接信息?

下面是 PowerShell SDK 相关 NuGet 包的概述改编自here

注意:System.Management.Automation推荐直接使用。

  • 创建独立应用程序 托管 PowerShell 运行 time 以便在进程中调用 PowerShell 命令:

    • .NET FrameworkWindows-仅)应用程序使用Microsoft.PowerShell.5.ReferenceAssemblies使用遗留 Windows PowerShell 运行time.

    • 使用Microsoft.PowerShell.SDK for (potentially cross-platform) .NET Core / 5+. applications using the PowerShell (Core) v6+运行时间。

      • 要确定特定的 .NET(核心)运行时间,给定的包版本必须至少与 select 链接页面上感兴趣的包版本相结合,展开 Dependencies 部分,并查阅第一个条目;例如,对于包版本 7.2.0,所需的最低 .NET(核心)运行时间是 net6.0
  • 创建实现 cmdlets 的程序集/构成模块的一部分以在 PowerShell 中使用 或创建 PowerShell 主机:

    • 使用PowerShellStandard.Library; it is compatible with both Windows PowerShell and PowerShell (Core) v6+, but note:

      • [This] 参考程序集 不包含任何实际实现 ,而是允许您仅使用存在于不同版本的 PowerShell 中的 API。这意味着 您仍然需要 运行 在 PowerShell 运行 时间内。"

至于通过 remoting 定位特定版本/版本:

  • 请注意,它涵盖了使用 PowerShell cmdlets 而非 SDK 的角度的远程处理,尽管您始终也可以通过 SDK 调用 cmdlets。


正如 Lee Daily 在评论中指出的那样,版本特定的 可执行文件名 是:

  • powershell.exe - Windows PowerShell

  • pwsh.exe (Windows) / pwsh(类 Unix 平台)- PowerShell(核心)v6+