运行 作为版本 2 的远程 Powershell 会话

Run remote Powershell session as version 2

我在 运行 Powershell 版本 2 的服务器上:

PS C:\> $PSVersionTable

Name      Value
----      -----
...
PSVersion 2.0

然后我创建一个新的远程会话到另一台计算机并连接到它:

$sess = New-PSSession -ComputerName {ComputerName} -Credential $credential

它 returns 我的结果:

PS C:\> Invoke-Command -Session $sess -ScriptBlock { $PSVersionTable }

Name      Value
----      -----
...
PSVersion 3.0

但是,我的脚本需要 Powershell 为版本 2,因此我进入了一个会话(以使其更容易)。然后我尝试让 Powershell 成为版本 2:

C:\> Enter-PSSession -Session $sess
[{ComputerName}]: PS C:\> Powershell -Version 2
Windows Powershell
Copyright (C) 2009 Microsoft Corporation. All rights reserverd

然后它就挂起(或者至少在我按 Ctrl-C 之前永远不会让我在控制台中输入任何其他内容)。

我也试过调用命令:

PS C:\> Invoke-Command -Session $sess -ScriptBlock { Powershell -version 2 }

它也一样。

我还尝试按照此处注册 PSSessionConfiguration:https://technet.microsoft.com/en-us/library/hh847899.aspx

PS C:\> Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0

但我得到:

Register-PSSessionConfiguration: a parameter cannot be found that matches parameter name 'PSVersion'.

有人知道我接下来可以尝试什么吗?! 谢谢

我认为以下也不起作用:

 #Requires -version 2.0

您可以尝试的另一种方法是在目标上创建一个计划任务,然后使用 Powershell.exe -version 2

启动您的脚本

你 运行 Register-PSSessionConfiguration 在什么机器上?您的计算机或 "server"?

您需要在目标服务器上进行配置。这就是您将要 运行 托管 PSSessionConfiguration 的内容。

我刚刚尝试了 technet article 中的步骤,效果非常好。我的 2008 服务器远程连接到我的 windows 7 机器 运行 宁一个 2.0 PSSessionConfiguration。

达到目标 server/host:

Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0

然后,在客户端机器上,引用 'PS2' 配置。

$s = New-PSSession -ComputerName Server01 -ConfigurationName PS2