powershell - 使用 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?
解决方案
如您的回答所示,过时的PowerShell Core 版本确实需要运行
Install-PowerShellRemoting.ps1
才能使给定计算机充当远程端点(即,允许其他计算机使用远程 cmdlet 来定位它,例如New-PSSession
or ),所述脚本Invoke-Command
是权宜之计当前的 PowerShell Core 版本不再需要。- 在撰写本文时,来自 PowerShell Core 中的WS -Management (WSMan) Remoting :
在我们向 Enable-PSRemoting 添加附加功能以执行相同操作之前,安装脚本是一个短期解决方案。
- 当前版本的 PowerShell Core 和所有后续版本都应该使用
Enable-PSRemoting
cmdlet,就像在 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
有关更多信息,请参阅这篇文章。
推荐阅读
- reactjs - 在 React 16 中无法通过 this.context 访问 contextType
- scala - 如何将该组列表的功能重新制作为简单递归?
- php - 在 WAMP 和 phppgadmin 上集成 postgreSQL
- javascript - 从 AngularJS 1.5 升级到 1.7 会抛出“无法复制!不支持复制 Window 或 Scope 实例”
- html - DevicePixelRatio 似乎突然与 Windows 比例叠加
- python - 获取二维数组中最近的坐标
- python - 为什么 vgg.prepare() 方法会创建给定图像的 9 个副本?
- python - 用于模式研究的 Python 正则表达式
- php - 如何显示输入数组的验证错误?
- ios - 在包含 collectionView 的 tableview XIB 中呈现 DocumentInteractionController