简体   繁体   English

如何让 winrm 默认使用 powershell 7 进行远程会话

[英]how to get winrm to use powershell 7 for remote sessions by default

With the release of powershell 7, seems like it is time to move past ps 5.1 so I have installed in on a couple of servers to give it a go.随着 powershell 7 的发布,似乎是时候超越 ps 5.1,所以我已经在几台服务器上安装了它来试一试。

However I when I create a session to these servers from my pc with ps7 I am always running ps5.1 on the remote machine.但是,当我使用 ps7 从我的电脑创建到这些服务器的会话时,我总是在远程机器上运行 ps5.1。

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

Which outputs 5.1.17763.316.输出 5.1.17763.316。 Any ideas how to get the remote session to use version 7.0.0 preferably by default?任何想法如何让远程会话在默认情况下最好使用 7.0.0 版?

Update making some progress with this, so though I would share.更新在这方面取得了一些进展,所以尽管我会分享。

On the remote machine in powershell 7 run the following command在 powershell 7 的远程机器上运行以下命令

Enable-PSRemoting

This will create some PsSessionConfigurations which you can see with the following command..这将创建一些 PsSessionConfigurations,您可以使用以下命令查看它们。

Get-PSSessionConfiguration

Now you can do the following to create sessions from powershell 7现在您可以执行以下操作以从 powershell 7 创建会话

Invoke-Command -ComputerName ServerName -ScriptBlock { $PsVersionTable.PSVersion } -ConfigurationName Powershell.7
$session = New-PSSession ServerName -ConfigurationName Powershell.7
Invoke-Command -Session $session -ScriptBlock { $PsVersionTable.PSVersion } 

This now uses ps 7 on the remote session, happy days.这现在在远程会话上使用 ps 7,快乐的日子。 Now how to make this happen by default...?现在如何在默认情况下实现这一点......? From this github issue :从这个github 问题

set the default microsoft.powershell endpoint to any PowerShell they choose将默认的 microsoft.powershell 端点设置为他们选择的任何 PowerShell

Which I think is what I want to do so switched back to ps 5.1 and tried this command:我认为这就是我想要做的,因此切换回 ps 5.1 并尝试了以下命令:

Get-PSSessionConfiguration -Name microsoft.powershell | Set-PSSessionConfiguration -PSVersion 7.0

Only to get the following output:只得到以下输出:

Set-PSSessionConfiguration : Cannot bind parameter 'PSVersion' to the target. Set-PSSessionConfiguration:无法将参数“PSVersion”绑定到目标。 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."异常设置“PSVersion”:“值 7.0 对于 PSVersion 参数无效。可用值为 2.0、3.0、4.0、5.0、5.1。”

though I would try this in ps7 so switched back by running pwsh and ran the same command again to get he following...虽然我会在 ps7 中尝试这个,所以通过运行pwsh切换回来并再次运行相同的命令让他跟随......

Write-Error: No session configuration matches criteria "microsoft.powershell".写入错误:没有会话配置符合条件“microsoft.powershell”。

So still not quite sure how to make ps7 the default... :(所以仍然不太确定如何使 ps7 成为默认值... :(

Note:笔记:

  • It is the remoting client that determines what remoting endpoint (session configuration) to connect to on the server machine - see below.远程处理客户端确定要连接到服务器计算机上的远程处理端点(会话配置) - 见下文。

  • Therefore, your own attempt,因此,你自己的尝试,

 # WRONG # 错误的\nGet-PSSessionConfiguration -Name microsoft.powershell | Get-PSSessionConfiguration -Name microsoft.powershell | \n  Set-PSSessionConfiguration -PSVersion 7.0设置-PSSessionConfiguration-PSVersion 7.0

is ineffective, because Set-PSSessionConfiguration modifies endpoint configurations on the server machine , it doesn't control the client 's behavior.无效,因为Set-PSSessionConfiguration修改服务器机器上的端点配置,它不控制客户端的行为。

Note that the fundamental prerequisite is that PowerShell remoting must be enabled on the server machine, which can be achieved either by opting to do so during installation via the MSI GUI installer, or by running Enable-PSRemoting - with admin privileges - later.请注意,基本的先决条件必须在服务器计算机上启用PowerShell 远程处理,这可以通过在安装期间通过 MSI GUI 安装程序选择这样做来实现,或者通过运行Enable-PSRemoting (具有管理员权限)来实现。 Tip of the hat to Lars Fosdal .Lars Fosdal 致敬

  • Doing so from PowerShell (Core) creates the standard session configuration(s) named PowerShell.<version> that clients can opt to connect to - see below.从 PowerShell (Core) 执行此操作会创建名为PowerShell.<version>的标准会话配置,客户端可以选择连接到该配置 - 见下文。

  • To list all configurations defined on a server, run Get-PSSessionConfiguration with admin privileges.要列出服务器上定义的所有配置, Get-PSSessionConfiguration使用管理员权限运行Get-PSSessionConfiguration


On a client machine , you can set a default for what session configuration defined on the server (remote machine) to connect to , via the $PSSessionConfigurationName preference variable .客户端计算机上,您可以通过$PSSessionConfigurationName首选项变量为服务器(远程计算机)上定义的会话配置设置默认连接

Eg, to target PowerShell 7 by default:例如,默认针对 PowerShell 7:

# When remoting, default to running PowerShell Core v7.x on the
# the target machines:
$PSSessionConfigurationName = 'PowerShell.7'

If you add the above to your $PROFILE file, future sessions will target PowerShell 7 by default.如果您将上述内容添加到$PROFILE文件中,默认情况下,未来的会话将针对 PowerShell 7。

See this answer for more information , which also shows how to target a given server configuration in the context of individual commands .有关更多信息,请参阅此答案,其中还显示了如何在单个命令的上下文中定位给定的服务器配置


Note: Changing what endpoint PowerShell [Core] targets by default - which as of 7.2 is still Window PowerShell - is being considered: see GitHub issue #11616 .注意:正在考虑更改默认情况下PowerShell [Core] 目标端点 - 从 7.2 开始仍然是Window PowerShell - 正在考虑中:请参阅GitHub 问题 #11616

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM