gpt4 book ai didi

powershell - 如何让 winrm 默认使用 powershell 7 进行远程 session

转载 作者:行者123 更新时间:2023-12-02 22:13:25 24 4
gpt4 key购买 nike

随着 powershell 7 的发布,似乎是时候超越 ps 5.1,所以我已经在几台服务器上安装了它来试一试。

但是,当我使用 ps7 从我的电脑创建到这些服务器的 session 时,我总是在远程机器上运行 ps5.1。

Invoke-Command -ComputerName name -ScriptBlock { 
Write-Host $env:COMPUTERNAME
$PSVersionTable.PsVersion
}

输出 5.1.17763.316。任何想法如何让远程 session 默认使用 7.0.0 版本?

更新
在这方面取得了一些进展,所以尽管我会分享。

在 powershell 7 的远程机器上运行以下命令
Enable-PSRemoting

这将创建一些 PsSessionConfigurations,您可以使用以下命令查看它们。
Get-PSSessionConfiguration

现在您可以执行以下操作以从 powershell 7 创建 session
Invoke-Command -ComputerName ServerName -ScriptBlock { $PsVersionTable.PSVersion } -ConfigurationName Powershell.7
$session = New-PSSession ServerName -ConfigurationName Powershell.7
Invoke-Command -Session $session -ScriptBlock { $PsVersionTable.PSVersion }

这现在在远程 session 中使用 ps 7,快乐的日子。现在如何在默认情况下实现这一点......?从这里 github issue :

set the default microsoft.powershell endpoint to any PowerShell they choose



我认为这就是我想要做的,因此切换回 ps 5.1 并尝试了以下命令:
Get-PSSessionConfiguration -Name microsoft.powershell | Set-PSSessionConfiguration -PSVersion 7.0

只得到以下输出:

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



虽然我会在 ps7 中尝试这个,所以通过运行 切换回来密码并再次运行相同的命令让他跟随......

Write-Error: No session configuration matches criteria "microsoft.powershell".



所以仍然不太确定如何使 ps7 成为默认值... :(

最佳答案

笔记:

  • 远程处理客户端确定要连接到的远程处理端点( session 配置)在服务器机器上 - 见下文。
  • 因此,你自己的尝试,

  • # WRONGGet-PSSessionConfiguration -Name microsoft.powershell |   Set-PSSessionConfiguration -PSVersion 7.0

    无效,因为 Set-PSSessionConfiguration 修改服务器机器上的端点配置 ,它不控制客户端的行为。
    请注意 基本前提必须启用 PowerShell 远程处理 在服务器机器上,这可以通过在安装期间通过 MSI GUI 安装程序选择这样做来实现,或者通过运行 Enable-PSRemoting 来实现。 - 具有管理员权限 - 稍后。感谢 Lars Fosdal .
  • 从 PowerShell (Core) 执行此操作会创建名为 PowerShell.<version> 的标准 session 配置。客户端可以选择连接到 - 见下文。
  • 要列出服务器上定义的所有配置,请运行 Get-PSSessionConfiguration 具有管理员权限。

  • 在客户端机器上 ,您可以 设置默认值 为了什么 在服务器(远程机器)上定义的 session 配置连接到 ,通过 $PSSessionConfigurationName preference variable .
    例如,默认情况下以 PowerShell 7 为目标:
    # When remoting, default to running PowerShell Core v7.x on the
    # the target machines:
    $PSSessionConfigurationName = 'PowerShell.7'
    如果您将上述内容添加到您的 $PROFILE文件, future 的 session 将默认针对 PowerShell 7。
    this answer更多信息 ,其中还显示 如何在单个命令的上下文中定位给定的服务器配置 .

    注意:正在考虑更改默认情况下 PowerShell [Core] 目标的端点 - 从 7.2 开始仍然是 Window PowerShell - 正在考虑中:参见 GitHub issue #11616 .

    关于powershell - 如何让 winrm 默认使用 powershell 7 进行远程 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60561852/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com