【问题标题】:Remote Execution of Powershell Command with PowerShell-Core Version使用 PowerShell-Core 版本远程执行 Powershell 命令
【发布时间】:2018-08-23 08:28:16
【问题描述】:

我正在尝试远程执行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?

【问题讨论】:

  • Install-PowerShellRemoting.ps1 应该是 PowerShell Core 安装的一部分。
  • @PetSerAl 不明白你的答案。我像任何其他程序一样安装了powershell核心,你在说什么这个脚本,更具体
  • 和任何其他程序安装一样,它会在您的系统中添加一些文件。其中有人应该被命名为Install-PowerShellRemoting.ps1

标签: powershell powershell-remoting powershell-core


【解决方案1】:
  • your answer 所示,已过时 PowerShell Core 版本确实需要运行 Install-PowerShellRemoting.ps1 才能使给定计算机充当远程端点(即允许其他计算机使用远程 cmdlet(例如 New-PSSessionInvoke-Command)将其作为目标,该脚本是当前 PowerShell Core 版本中不再需要的权宜之计

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

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

注意:在撰写本文时,WS-Management (WSMan) Remoting in PowerShell Core 尚未提及当前 6.x 版本不再需要权宜之计脚本 - see this GitHub issue。另请注意,Enable-PSRemoting类 Unix 平台上不可用,自 v7.0 起(也不是过时的 Install-PowerShellRemoting.ps1 脚本)。


顺便说一句,针对特定 PowerShell Core 版本

虽然-ConfigurationName Powershell.6.0.4 确实可以在您想要定位非常特定版本 时工作,但Enable-PSRemoting 也会创建一个PowerShell.<OwnMajorVersionOnly> 配置,不需要您指定完整版本编号(可能会随着您在端点计算机上更新 PowerShell Core 而发生变化)。

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

-ConfigurationName PowerShell.7

如需了解更多信息,请参阅this post

【讨论】:

    【解决方案2】:

    在powershell core的安装文件夹中,有一个脚本文件Install-PowerShellRemoting

    在您希望会话连接到的远程服务器中运行脚本, 而不是在远程使用时:

    $session = New-PSSession -ComputerName IIS-DEV2 -port 5985 -Credential $mycreds -ConfigurationName "powershell.6.0.4"
    

    它奏效了,让我与 Powershell 核心进行了一次会话。

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 2021-12-22
      • 2021-07-28
      • 2012-01-16
      • 2018-05-27
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多