首页 > 解决方案 > 使用 PowerShell-Core 版本远程执行 Powershell 命令

问题描述

我正在尝试远程执行 Powershell 命令,我的问题是使用的 Powershell 是 4.0 版,我想使用 Powershell Core 6.0.4 远程执行我的命令。

我试图在远程主机上使用这个命令

Set-PSSessionConfiguration -name microsoft.powershell -psversion 6.0 -FORCE

并得到这个错误:

Set-PSSessionConfiguration : Cannot bind parameter 'PSVersion' to the target. Exception setting "PSVersion": "The
value 6.0 is not valid for the PSVersion parameter. The available values are 2.0,3.0 and 4.0."

我在远程机器上安装了 6.0.4 版本。

我知道它使用版本 4 来执行我的远程命令,因为

 Invoke-Command -Session $session -ScriptBlock {$PSVersionTable.PSVersion};

返回:

Major  Minor  Build  Revision PSComputerName
-----  -----  -----  -------- --------------
4      0      -1     -1       IIS-DEV2

任何想法如何强制它使用版本 6?

标签: powershellpowershell-remotingpowershell-core

解决方案


  • 您的回答所示,过时的PowerShell Core 版本确实需要运行Install-PowerShellRemoting.ps1才能使给定计算机充当远程端点(即,允许其他计算机使用远程 cmdlet 来定位它,例如New-PSSessionor ),所述脚本Invoke-Command权宜之计当前的 PowerShell Core 版本不再需要

在我们向 Enable-PSRemoting 添加附加功能以执行相同操作之前,安装脚本是一个短期解决方案。

  • 当前版本的 PowerShell Core 和所有后续版本都应该使用Enable-PSRemotingcmdlet,就像在 Windows PowerShell 中一样。

注意:在撰写本文时,PowerShell Core 中的 WS-Management (WSMan) Remoting尚未提及当前 6.x 版本不再需要权宜之计脚本 -请参阅此 GitHub 问题。另请注意,从 v7.0开始,它在类 UnixEnable-PSRemoting平台上不可用(也不是过时的Install-PowerShellRemoting.ps1脚本)。


顺便说一句,重新定位特定的PowerShell Core 版本

-ConfigurationName Powershell.6.0.4如果您想针对一个非常特定的版本,虽然确实有效,但Enable-PSRemoting也会创建一个PowerShell.<OwnMajorVersionOnly>不需要您指定完整版本号的配置(当您在端点计算机上更新 PowerShell Core 时,它​​可能会发生变化)。

因此,请考虑改用以下内容,使用Enable-PSRemoting从远程端点上的 PowerShell 7.x 版本运行的示例:

-ConfigurationName PowerShell.7

有关更多信息,请参阅这篇文章


推荐阅读