Windows PowerShell 设计上是向下兼容之前版本的。 为 Windows PowerShell 2.0 编写的指令集、提供者、snap-in、模块和脚本在较新版本的 Windows PowerShell 中运行不变。 然而,Microsoft .NET Framework 4 更改了运行时激活策略。 为 Windows PowerShell 2.0 编写并使用通用语言运行时(CLR) 2.0 编译的 Windows PowerShell 托管程序,在使用 CLR 4.0(或更高版本)编译的 Windows PowerShell 中,未经修改无法运行。
Windows PowerShell 2.0 引擎仅在现有脚本或主机程序因与 Windows PowerShell 5.1 不兼容而无法运行时使用。 例如旧版本的Exchange或SQL Server模块。 此类案件预计较为罕见。
许多需要Windows PowerShell 2.0引擎的程序会自动启动它。 这些说明适用于极少数需要手动启动发动机的情况。
弃用与安全问题
Windows PowerShell 2.0 于 2017 年 8 月被弃用。 更多信息请参见PowerShell博客上的 公告 。
Windows PowerShell 2.0缺少了3、4和5版本中添加的大量加固和安全功能。 我们强烈建议用户如果能避免,不要使用它。 更多信息请参见《 Shell与脚本语言安全性比较 》和《 PowerShell ♥蓝队》。
安装和启用所需程序
在启动 Windows PowerShell 2.0 引擎之前,启用 Windows PowerShell 2.0 引擎和 Microsoft .NET Framework 3.5(服务包 1)。 有关说明,请参见 安装Windows PowerShell。
安装Windows Management Framework 3.0或更高版本的系统具备所有必需组件。 无需进一步配置。 有关安装 Windows 管理框架的信息,请参见 安装和配置 WMF。
如何启动Windows PowerShell 2.0引擎
启动 Windows PowerShell 时,默认是最新版本。 要使用Windows PowerShell 2.0引擎启动Windows PowerShell,请使用Version参数。PowerShell.exe 你可以在任何命令提示符下运行该命令,包括 Windows PowerShell 和 Cmd.exe。
PowerShell.exe -Version 2
如何用 Windows PowerShell 2.0 引擎启动远程会话
要在远程会话中运行 Windows PowerShell 2.0 引擎,请在加载 Windows PowerShell 2.0 引擎的远程计算机上创建一个会话配置(也称为 端点)。 会话配置保存在远程计算机上,任何授权用户都可以使用创建使用 Windows PowerShell 2.0 引擎的会话。
这是一项高级任务,通常由系统管理员执行。
以下过程使用 Register-PSSessionConfiguration cmdlet 中的 PSVersion 参数,创建使用 Windows PowerShell 2.0 引擎的会话配置。 你还可以使用 New-PSSessionConfigurationFile cmdlet 中的 PowerShellVersion 参数,为加载 Windows PowerShell 2.0 引擎的会话创建会话配置文件,并且可以使用 Set-PSSessionConfiguration 参数中的 PSVersion 参数来更改会话配置以使用 Windows PowerShell 2.0 引擎。
有关会话配置文件的详细信息,请参阅 about_Session_Configuration_Files。 有关会话配置(包括设置和安全)的信息,请参见 about_Session_Configurations。
启动远程Windows PowerShell 2.0会话
要创建需要 Windows PowerShell 2.0 引擎的会话配置,请使用 命令小子的
Register-PSSessionConfigurationPSVersion 参数,值为2.0。 在连接的“服务器端”或接收端的电脑上执行此命令。以下示例命令在 Server01 计算机上创建 PS2 会话配置。 要执行此命令,启动Windows PowerShell,使用“ 以管理员身份运行 ”选项。
Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0要在使用 PS2 会话配置的 Server01 计算机上创建会话,可以使用创建远程会话的 cmdlet 中的 ConfigurationName 参数,例如 'New-PSSession cmdlet。
当使用该会话配置的会话启动时,Windows PowerShell 2.0 引擎会自动加载到该会话中。
以下命令在使用 PS2 会话配置的 Server01 计算机上启动会话。 该命令将会话保存在变量中
$s。$s = New-PSSession -ComputerName Server01 -ConfigurationName PS2
如何用Windows PowerShell 2.0引擎启动后台作业
要启动Windows PowerShell 2.0引擎的后台作业,请使用Start-Job命令中的PSVersion参数。
以下命令启动Windows PowerShell 2.0引擎的后台作业
Start-Job {Get-Process} -PSVersion 2.0
有关背景工作的更多信息,请参见 about_Jobs。