【问题标题】:How to Programmatically Set Up Powershell Remoting on an Azure VM如何以编程方式在 Azure VM 上设置 Powershell 远程处理
【发布时间】:2016-09-03 01:23:54
【问题描述】:

我的目标是编写一个运行 New-AzureRmResourceGroupNew-AzureRmResourceGroupDeployment 的 Powershell 脚本,以便根据 ARM .json 模板文件配置资源组。所述资源组包括虚拟机、虚拟网络、网络安全组、公共IP地址、附加到虚拟机的网络接口和两个存储帐户。之后,我希望继续执行相同的脚本并将特定程序安装程序复制到该资源组中的虚拟机并自动运行该安装程序,而无需进一步的用户交互。但是,我似乎无法开始与虚拟机的远程 Powershell 会话。我运行命令:

$sess = New-PSSession -ComputerName **.***.**.*** -Port XXXX -Credential $cred

*s 是虚拟机的 IP 地址; XXXX 是根据与虚拟机所在的虚拟网络关联的网络安全组为 RDP 开放的端口; $cred 包含虚拟机上管理员用户的凭据。

命令总是返回错误:

New-PSSession : [**.***.**.***] Connecting to remote server **.***.**.*** failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.

请注意,我确实确保将虚拟机的 IP 地址添加到本地计算机上的受信任主机列表中。 (在我这样做之前,我收到一条不同的错误消息。)另外,如果我在 Azure 门户中单击其连接按钮,然后单击 .rdp,我能够连接到虚拟机下载的文件。此远程会话使用与我在 Powershell 中尝试但未能设置的相同的 IP 地址、端口和凭据。这是我不明白的。

为什么会这样?我需要做一些额外的工作来准备 VM 以接受远程 Powershell 会话吗?有什么方法可以在 ARM 模板中配置它,以便 VM 从一开始就准备好接受它们? (如果我需要在 VM 上运行一些命令来设置 Powershell 远程处理会很困难,因为由于这个问题,我无法远程运行 Powershell 来运行它们。也许我可以将它们作为自定义脚本扩展运行?)

最后说明:此 VM 是“新”样式,而不是“经典”样式。我知道有很多关于“经典”风格的 Azure VM 的文档,但这不是我正在使用的。此外,即使按照错误提示在 VM 上运行 winrm quickconfig,并向本地用户远程启用管理权限,我在运行 New-PSSession 时也会遇到同样的错误。

【问题讨论】:

    标签: powershell azure virtual-machine remote-desktop


    【解决方案1】:
    1. 在您的 NSG 上打开 5985-5986
    2. 删除命令中的端口部分:

      $sess = New-PSSession -ComputerName ... -Credential $cred

    WinRM 端点会自动为新 VM 设置(如果您不以某种方式覆盖它)。但是您需要为非安全打开 5985,为安全远程处理打开 5986

    【讨论】:

    • 这次我遇到了一个不同的错误:New-PSSession : [40.123.50.2] Connecting to remote server 40.123.50.2 failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. 我最终不得不进入虚拟机上的 Windows 防火墙并在任何地方为公共配置文件启用“Windows 远程管理 (Http-In)”,而不仅仅是在子网上。有没有办法在 ARM 模板中做到这一点?
    • 现在这很奇怪,我什至不记得必须这样做一次。至于手臂部分,我从来没有听说过,快速的互联网搜索并没有发现类似的东西,但这并不意味着它不可能。你可能不会。顺便说一句,winrm qc 配置防火墙(但不用于公共配置文件)
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2016-11-21
    • 2019-05-09
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多