You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PowerShell 7中为WSMan注册Powershell.7端点以使用Invoke-Command

解决PowerShell 7与WSMan远程配合的问题

一、修复WinRM基础配置

你执行Set-WSManQuickConfig失败,可手动完成WinRM的核心配置:

  1. 以管理员身份启动PowerShell 7
  2. 确保WinRM服务正常运行并设置为自动启动:
    Start-Service WinRM
    Set-Service WinRM -StartupType Automatic
    
  3. 检查是否存在WSMan监听器:
    Get-WSManInstance -ResourceURI winrm/config/listener
    
    若未返回结果,手动创建HTTP监听器:
    New-WSManInstance -ResourceURI winrm/config/listener -SelectorSet @{Address="*"; Transport="HTTP"}
    
  4. 启用WinRM的防火墙允许规则:
    Enable-NetFirewallRule -Name "WINRM-HTTP-In-TCP"
    

二、正确注册PowerShell 7的WSMan端点

PowerShell Core(含7.x版本)不支持-PowerShellVersion参数,按以下步骤注册端点:

  1. 保持管理员权限的PowerShell 7窗口,执行:
    Register-PSSessionConfiguration -Name "PowerShell.7" -Force
    
    该命令会创建一个绑定当前PowerShell 7版本的会话配置端点。
  2. 验证端点注册结果:
    Get-PSSessionConfiguration
    
    此时应能看到名为PowerShell.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 17:28:26