如何在PowerShell 7中为WSMan注册Powershell.7端点以使用Invoke-Command
解决PowerShell 7与WSMan远程配合的问题
一、修复WinRM基础配置
你执行Set-WSManQuickConfig失败,可手动完成WinRM的核心配置:
- 以管理员身份启动PowerShell 7
- 确保WinRM服务正常运行并设置为自动启动:
Start-Service WinRM Set-Service WinRM -StartupType Automatic - 检查是否存在WSMan监听器:
若未返回结果,手动创建HTTP监听器:Get-WSManInstance -ResourceURI winrm/config/listenerNew-WSManInstance -ResourceURI winrm/config/listener -SelectorSet @{Address="*"; Transport="HTTP"} - 启用WinRM的防火墙允许规则:
Enable-NetFirewallRule -Name "WINRM-HTTP-In-TCP"
二、正确注册PowerShell 7的WSMan端点
PowerShell Core(含7.x版本)不支持-PowerShellVersion参数,按以下步骤注册端点:
- 保持管理员权限的PowerShell 7窗口,执行:
该命令会创建一个绑定当前PowerShell 7版本的会话配置端点。Register-PSSessionConfiguration -Name "PowerShell.7" -Force - 验证端点注册结果:
此时应能看到名为Get-PSSessionConfigurationPowerShell.7的配置项。
三、测试远程命令执行
使用注册好的端点运行测试命令:
$server = 'localhost' Invoke-Command -Authentication Negotiate -ConfigurationName PowerShell.7 -ComputerName $server { Write-Host "当前PowerShell版本:$($PSVersionTable.PSVersion)" }
若成功,会返回PowerShell 7的版本号,说明端点工作正常。
补充说明
- 你之前省略
-ConfigurationName的命令能运行,但默认调用的是Windows PowerShell 5.1的端点,因此使用的不是目标版本。 - 尽管PowerShell 7官方推荐使用SSH远程,但在Windows Server 2012 R2上确实可以通过WSMan实现远程管理,只需正确注册对应端点即可。
内容的提问来源于stack exchange,提问作者Victor Sanchez
相关产品推荐
相关产品推荐

