[玩转系统] 关于 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 的 Desktop
和 Core
版本兼容:
@{
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 模块以实现版本交叉兼容性
在编写针对 Desktop
和 Core
版本 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 版本的模块
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag