简体   繁体   English

如何以编程方式在Azure VM上设置Powershell远程处理

[英]How to Programmatically Set Up Powershell Remoting on an Azure VM

My goal is to write a Powershell script that will run New-AzureRmResourceGroup and New-AzureRmResourceGroupDeployment in order to provision a resource group according to an ARM .json template file. 我的目标是编写一个Powershell脚本,该脚本将运行New-AzureRmResourceGroupNew-AzureRmResourceGroupDeployment ,以便根据ARM .json模板文件配置资源组。 Said resource group includes a virtual machine, virtual network, network security group, public IP address, network interface attached to the virtual machine, and two storage accounts. 所述资源组包括虚拟机,虚拟网络,网络安全组,公共IP地址,连接到虚拟机的网络接口以及两个存储帐户。 After that, I want the same script to go on and copy a specific program installer to the virtual machine in that resource group and run that installer, automatically without further user interaction. 之后,我希望继续执行相同的脚本,然后将特定的程序安装程序复制到该资源组中的虚拟机,然后自动运行该安装程序,而无需进一步的用户交互。 However, I can't seem to begin a remote Powershell session with the virtual machine. 但是,我似乎无法与虚拟机开始远程Powershell会话。 I run the command: 我运行命令:

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

where the * s are the IP address of the Virtual Machine; *是虚拟机的IP地址; XXXX is the Port that is open for RDP according to the Network Security Group associated with the virtual network that the Virtual Machine is on; XXXX是根据与虚拟机所在的虚拟网络关联的网络安全组为RDP打开的端口; and $cred contains the credentials of the admin user on the Virtual Machine. $cred包含虚拟机上admin用户的凭据。

The command always returns an error: 该命令始终返回错误:

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.

Note that I did make sure to add the Virtual Machine's IP address to the list of Trusted-Hosts on my local machine. 请注意,我确实确保将虚拟机的IP地址添加到本地计算机上的“受信任的主机”列表中。 (Until I did that, I got a different error message.) Also, I am able to connect to the Virtual Machine if I click on its Connect button in the Azure portal and then click on the .rdp file that's downloaded. (直到我这样做,我得到一个不同的错误信息。)另外, 能够连接到虚拟机,如果我在Azure的门户网站点击它的连接按钮,然后单击,就会向下载的.rdp文件。 This remote session uses the same IP address, port, and credentials as the one I'm trying and failing to set up in Powershell. 该远程会话使用与我在Powershell中尝试但无法设置的IP地址,端口和凭据相同的IP地址,端口和凭据。 This is what I don't understand. 这是我不明白的。

连接按钮产生有效的.rdp

Why does that happen? 为什么会这样呢? Is there some additional work I need to do to prepare the VM for accepting remote Powershell sessions? 我需要做一些其他工作来准备VM接受远程Powershell会话吗? Is there any way to configure it in the ARM template so that the VM will be ready to accept them from the get-go? 有什么方法可以在ARM模板中对其进行配置,以便VM可以从一开始就接受它们? (It would be difficult if I need to run some commands on the VM to set Powershell remoting up, since I can't Powershell remote in to run them because of this very problem. Maybe I could run them as a custom script extension?) (如果需要在VM上运行一些命令来设置Powershell远程处理,这将很困难,因为由于这个问题,我无法远程运行Powershell来运行它们。也许我可以将它们作为自定义脚本扩展来运行?)

Final notes: This VM is "new" style, not "classic" style. 最后说明:此VM是“新”样式,而不是“经典”样式。 I know there is lots of documentation out there for "classic" style Azure VMs, but that's not what I'm working with. 我知道有很多关于“经典”风格的Azure VM的文档,但这不是我正在使用的文档。 Also, even after running winrm quickconfig on the VM as the error suggested, and enabling administrative rights remotely to local users, I get the same error when I run New-PSSession . 另外,即使按照错误提示在VM上运行winrm quickconfig并远程向本地用户启用管理权限,运行New-PSSessionwinrm quickconfig遇到相同的错误。

  1. Open up 5985-5986 on your NSG 在您的NSG上打开5985-5986
  2. Drop the port part on your command: 将端口部分放在命令中:

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

WinRM endpoint is set up automatically for new VM's (if you don't somehow override it). WinRM端点是自动为新VM设置的(如果您不以某种方式覆盖它)。 But you need to open 5985 for nonsecure and 5986 for secure remoting 但是您需要打开5985(不安全)和5986(安全)

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

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