简体   繁体   English

如何使用 PowerShell 通过 WinRM 连接到 Azure VM

[英]How to connect to Azure VMs via WinRM using PowerShell

I have two Azure VMs, i need to connect to them via WinRM from my local computer and deploy IIS website on VMs via PowerShell script.我有两个 Azure 虚拟机,我需要从本地计算机通过 WinRM 连接到它们,并通过 PowerShell 脚本在虚拟机上部署 IIS 网站。 The main problem is that I do not understand how to connect to VMs from the local computer via WinRM, since the machines do not have a public IP address, and the connection to them goes through a public address from LoadBalancer.主要问题是我不明白如何通过 WinRM 从本地计算机连接到虚拟机,因为这些机器没有公共 IP 地址,并且与它们的连接通过 LoadBalancer 的公共地址进行。

• I would suggest you use a custom script extension with your Virtual machines and execute a powershell script that will help you to connect to the VM using the certificate stored in the key vault and further ensure that the 'WinRM' service is running successfully on the VM as below to connect through the Windows Remote Management service to the VM for executing a certain script: - • 我建议您对虚拟机使用自定义脚本扩展并执行 powershell 脚本,该脚本将帮助您使用存储在密钥保管库中的证书连接到 VM,并进一步确保“WinRM”服务在VM如下,通过Windows远程管理服务连接到VM执行某个脚本:-

Enable-PSRemoting -Force
Get-Service WinRM

You can use the below powershell script to create an Azure keyvault, create a certificate store in it, generate a secret URL for it and connect to the VM using this configuration: -您可以使用以下 powershell 脚本创建 Azure 密钥库,在其中创建证书存储,生成秘密 URL 并使用此配置连接到它

$vm = New-AzVMConfig -VMName "<VM name>" -VMSize "<VM Size>"
$credential = Get-Credential
$secretURL = (Get-AzKeyVaultSecret -VaultName "<vault name>" -Name "<secret name>").Id
$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName "<Computer Name>" -Credential $credential -WinRMHttp -WinRMHttps -ProvisionVMAgent -WinRMCertificateUrl $secretURL
$sourceVaultId = (Get-AzKeyVault -ResourceGroupName "<Resource Group name>" -VaultName "<Vault Name>").ResourceId
$CertificateStore = "My"
$vm = Add-AzVMSecret -VM $vm -SourceVaultId $sourceVaultId -CertificateStore $CertificateStore -CertificateUrl $secretURL

Please find the below link for more clarification in this regard: -有关这方面的更多说明,请找到以下链接:-

https://docs.microsoft.com/en-us/azure/virtual-machines/windows/connect-winrm https://docs.microsoft.com/en-us/azure/virtual-machines/windows/connect-winrm

• Ensure that ports 5985 for WinRM HTTP and 5986 for WinRM HTTPS as well as port 80 and 443 for the respective WinRM HTTP and HTTPS connections to be made are allowed through the load balancer for inbound as well as outbound connections for WinRM service . • Ensure that ports 5985 for WinRM HTTP and 5986 for WinRM HTTPS as well as port 80 and 443 for the respective WinRM HTTP and HTTPS connections to be made are allowed through the load balancer for inbound as well as outbound connections for WinRM service .

• Also, you can connect to your VMs configured with a private IP address through a jump VM that is configured with a public IP address, but you still will have to enable custom script extension to execute a powershell command remotely on them. If these VMs are joined to a domain, then ensure that the below group policies are not applied on them as these will not enable WinRM services to be executed successfully on them as well as the 'WinRM' service is started and the respective group policy is enabled successfully on these VMs • 此外, you can connect to your VMs configured with a private IP address through a jump VM that is configured with a public IP address, but you still will have to enable custom script extension to execute a powershell command remotely on them. If these VMs are joined to a domain, then ensure that the below group policies are not applied on them as these will not enable WinRM services to be executed successfully on them as well as the 'WinRM' service is started and the respective group policy is enabled successfully on these VMs you can connect to your VMs configured with a private IP address through a jump VM that is configured with a public IP address, but you still will have to enable custom script extension to execute a powershell command remotely on them. If these VMs are joined to a domain, then ensure that the below group policies are not applied on them as these will not enable WinRM services to be executed successfully on them as well as the 'WinRM' service is started and the respective group policy is enabled successfully on these VMs : - you can connect to your VMs configured with a private IP address through a jump VM that is configured with a public IP address, but you still will have to enable custom script extension to execute a powershell command remotely on them. If these VMs are joined to a domain, then ensure that the below group policies are not applied on them as these will not enable WinRM services to be executed successfully on them as well as the 'WinRM' service is started and the respective group policy is enabled successfully on these VMs :-

Computer Configuration --> Policies --> Administrative Templates: Policy definitions --> Windows Components --> Windows Remote Management (WinRM) --> WinRM Service --> Allow remote server management through WinRM --> Enabled

Please find the below link for more clarification in this regard: -有关这方面的更多说明,请找到以下链接:-

https://support.auvik.com/hc/en-us/articles/204424994-How-to-enable-WinRM-with-domain-controller-Group-Policy-for-WMI-monitoring https://support.auvik.com/hc/en-us/articles/204424994-How-to-enable-WinRM-with-domain-controller-Group-Policy-for-WMI-monitoring

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

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