当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 关于 PowerShell 版本

作者:精品下载站 日期:2024-12-14 02:20:33 浏览:15 分类:玩电脑

关于 PowerShell 版本


简短描述

不同版本的 PowerShell 在不同的底层运行时上运行。

详细描述

从 PowerShell 5.1 开始,PowerShell 有多个版本,每个版本都在不同的 .NET 运行时上运行。从 PowerShell 6.0 开始,PowerShell 有两个版本:

  • 桌面,在 .NET Framework 上运行。 PowerShell 4 及更低版本以及 PowerShell 5.1 可用于全功能 Windows 版本,例如 Windows Desktop、Windows Server、Windows Server Core 和大多数其他 Windows 操作系统。这是原始的 PowerShell 版本,包含在操作系统的默认安装中。
  • Core,在 .NET Core 上运行。 PowerShell 6.0 及更高版本与早期 PowerShell 版本并行安装在全功能 Windows 版本、一些占用空间较小的 Windows 版本(例如 Windows Nano Server 和 Windows IoT)或非 Windows 平台(例如 Linux 和 macOS)上。

由于PowerShell的版本与其.NET运行时相对应,因此它是.NET API和PowerShell模块兼容性的首要指标;某些 .NET API、类型或方法在 .NET 运行时中不可用,这会影响依赖于它们的 PowerShell 脚本和模块。

$PSEdition 自动变量

在 PowerShell 5.1 及更高版本中,您可以使用 $PSEdition 自动变量了解您正在运行的版本:

$PSEdition
Core

在 PowerShell 4 及更低版本中,此变量不存在。 $PSEdition 为 null 应被视为与具有值 Desktop 相同。

$PSVersionTable 中的版本

在 PowerShell 5.1 及更高版本中,$PSVersionTable 自动变量还具有 PSEdition 属性:

$PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.3.9
PSEdition                      Core
GitCommitId                    7.3.9
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PSEdition 字段与 $PSEdition 自动变量具有相同的值。

CompatiblePSEditions 模块清单字段

PowerShell 模块可以使用模块清单的 CompatiblePSEditions 字段声明它们与哪些版本的 PowerShell 兼容。

例如,模块清单声明与 PowerShell 的 DesktopCore 版本兼容:

@{
    ModuleVersion = '1.0'
    FunctionsToExport = @('Test-MyModule')
    CompatiblePSEditions = @('Desktop', 'Core')
}

仅兼容 Desktop 的模块清单示例:

@{
    ModuleVersion = '1.0'
    FunctionsToExport = @('Test-MyModule')
    CompatiblePSEditions = @('Desktop')
}

从模块清单中省略 CompatiblePSEditions 字段与将其设置为 Desktop 具有相同的效果,因为在引入此字段之前创建的模块是为此版本隐式编写的。

对于不作为 Windows 一部分提供的模块(即您从库中编写或安装的模块),此字段仅供参考; PowerShell 不会根据 CompatiblePSEditions 字段更改行为,但会将其公开在 PSModuleInfo 对象(由 Get-Module 返回)上供您自己使用逻辑:

New-ModuleManifest -Path .\TestModuleWithEdition.psd1 -CompatiblePSEditions Desktop,Core -PowerShellVersion '5.1'
$ModuleInfo = Test-ModuleManifest -Path .\TestModuleWithEdition.psd1
$ModuleInfo.CompatiblePSEditions
Desktop
Core

笔记

CompatiblePSEditions 模块字段仅与 PowerShell 5.1 及更高版本兼容。包含此字段将导致模块与 PowerShell 4 及更低版本不兼容。由于该字段纯粹是提供信息的,因此可以在更高版本的 PowerShell 中安全地省略它。

在 PowerShell 6.1 中,Get-Module -ListAvailable 已更新其格式化程序,以显示每个模块的版本兼容性:

Get-Module -ListAvailable
    Directory: C:\Users\me\Documents\PowerShell\Modules

ModuleType Version    Name                   PSEdition ExportedCommands
---------- -------    ----                   --------- ----------------
Script     1.4.0      Az                     Core,Desk
Script     1.3.1      Az.Accounts            Core,Desk {Disable-AzDataCollection, Disable-AzContextAutosave, E...
Script     1.0.1      Az.Aks                 Core,Desk {Get-AzAks, New-AzAks, Remove-AzAks, Import-AzAksCreden...

...

Script     4.4.0      Pester                 Desk      {Describe, Context, It, Should...}
Script     1.18.0     PSScriptAnalyzer       Desk      {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-...
Script     1.0.0      WindowsCompatibility   Core      {Initialize-WinSession, Add-WinFunction, Invoke-WinComm...

作为 Windows 一部分提供的模块的版本兼容性

对于作为 Windows 一部分提供的模块(或作为角色或功能的一部分安装),PowerShell 6.1 及更高版本以不同方式处理 CompatiblePSEditions 字段。此类模块可在 Windows PowerShell 系统模块目录 (%windir%\System\WindowsPowerShell\v1.0\Modules) 中找到。

对于从此目录加载或在此目录中找到的模块,PowerShell 6.1 及更高版本使用 CompatiblePSEditions 字段来确定该模块是否与当前会话兼容并相应地运行。

使用 Import-Module 时,CompatiblePSEditions 中没有 Core 的模块将不会被导入,并会显示错误:

Import-Module BitsTransfer
Import-Module : Module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.psd1'
 does not support current PowerShell edition 'Core'. Its supported editions are 'Desktop'. Use 'Import-Module
 -SkipEditionCheck' to ignore the compatibility of this module.
At line:1 char:1
+ Import-Module BitsTransfer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (C:\WINDOWS\system32\u2026r\BitsTransfer.psd1:String)
 [Import-Module], InvalidOperationException
+ FullyQualifiedErrorId : Modules_PSEditionNotSupported,Microsoft.PowerShell.Commands.ImportModuleCommand

使用 Get-Module -ListAvailable 时,CompatiblePSEditions 中没有 Core 的模块将不会显示:

Get-Module -ListAvailable BitsTransfer
# No output

在这两种情况下,-SkipEditionCheck 开关参数可用于覆盖此行为:

Get-Module -ListAvailable -SkipEditionCheck BitsTransfer
    Directory: C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

ModuleType Version    Name           PSEdition ExportedCommands
---------- -------    ----           --------- ----------------
Manifest   2.0.0.0    BitsTransfer   Desk      {Add-BitsFile, Complete-BitsTransfer, Get-BitsTransfer,...

警告

对于某个模块,Import-Module -SkipEditionCheck 可能看起来成功,但使用该模块可能会面临稍后遇到不兼容的风险;虽然加载模块最初成功,但命令可能稍后调用不兼容的 API 并自发失败。

编写 PowerShell 模块以实现版本交叉兼容性

在编写针对 DesktopCore 版本 PowerShell 的 PowerShell 模块时,您可以采取一些措施来确保跨版本兼容性。

然而,确认和持续验证兼容性的唯一真正方法是为您的脚本或模块编写测试,并在您需要兼容的 PowerShell 的所有版本上运行它们。推荐的测试框架是 Pester。

PowerShell 脚本

作为一种语言,PowerShell 在不同版本之间的工作方式相同;您使用的 cmdlet、模块和 .NET API 会受到版本兼容性的影响。

通常,在 PowerShell 6.1 及更高版本中运行的脚本也可以在 Windows PowerShell 5.1 中运行,但也有一些例外。

PSScriptAnalyzer 版本 1.18+ 具有 PSUseCompatibleCommands 和 PSUseCompatibleTypes 等规则,能够检测 PowerShell 脚本中命令和 .NET API 可能不兼容的用法。

.NET 程序集

如果您正在编写二进制模块或包含从源代码生成的 .NET 程序集 (DLL) 的模块,则应根据 .NET 标准和 PowerShell 标准进行编译,以对 .NET 和 PowerShell API 兼容性进行编译时兼容性验证。

尽管这些库能够在编译时检查某些兼容性,但它们无法捕获版本之间可能存在的行为差异。为此,您仍然必须编写测试。

参见

  • about_Automatic_Variables
  • Get-Module
  • Import-Module
  • 具有兼容 PowerShell 版本的模块

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯