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

本地无法通过PowerShell访问Azure Windows VM执行Get-Date脚本报错

解决Azure Windows VM PowerShell远程连接(WinRM)失败问题

1. 验证VM网络连通性

  • 本地执行以下命令测试WinRM端口(默认HTTP用5985,HTTPS用5986)是否可达:
    Test-NetConnection 57.151.88.183 -Port 5985
    
  • 若端口不通,检查Azure VM的网络安全组(NSG):
    • 确认NSG入站规则允许你的本地IP访问WinRM端口(不要仅限制同一子网)。
    • 核对VM的公共IP是否为57.151.88.183,避免IP变更导致连接失败。

2. 检查VM上的WinRM服务与防火墙配置

通过Azure门户的串行控制台登录VM,执行以下操作:

  • 确认WinRM服务运行状态:
    Get-Service WinRM
    
    状态需为Running,若未启动则执行Start-Service WinRM并设置开机自启:Set-Service WinRM -StartupType Automatic。
  • 修改WinRM公共防火墙规则,允许外部IP访问:
    Set-NetFirewallRule -Name WINRM-HTTP-In-TCP-PUBLIC -RemoteAddress Any
    
    (若使用HTTPS,对应修改规则WINRM-HTTPS-In-TCP-PUBLIC)
  • 验证WinRM监听配置,确保存在面向公网的监听:
    winrm enumerate winrm/config/listener
    
    需看到Address = *或包含VM公共IP的监听条目。

3. 本地PowerShell配置调整

  • 检查并添加VM IP到本地WinRM信任主机列表:
    # 查看当前信任列表
    Get-Item WSMan:\localhost\Client\TrustedHosts
    # 添加目标VM IP(-Concatenate用于保留现有条目)
    Set-Item WSMan:\localhost\Client\TrustedHosts -Value "57.151.88.183" -Concatenate
    
  • 确保以管理员身份运行PowerShell,使用正确的远程连接命令:
    # 交互式会话
    Enter-PSSession -ComputerName 57.151.88.183 -Credential (Get-Credential)
    # 直接执行Get-Date命令
    Invoke-Command -ComputerName 57.151.88.183 -ScriptBlock {Get-Date} -Credential (Get-Credential)
    

4. Azure VM远程管理初始化(首次配置)

若VM未配置过WinRM,可通过Azure扩展快速初始化:

  1. 在Azure门户进入VM的扩展页面,点击“添加”。
  2. 选择“自定义脚本扩展”,上传或输入以下初始化脚本:
    winrm quickconfig -quiet
    winrm set winrm/config/client '@{TrustedHosts="*"}'
    Set-NetFirewallRule -Name WINRM-HTTP-In-TCP-PUBLIC -RemoteAddress Any
    
  3. 运行扩展完成配置。

内容的提问来源于stack exchange,提问作者MAULIKKUMAR PATEL

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 13:03:13