Remote PowerShell to AzureRM Virtual Machines

若如初见. 提交于 2019-12-21 05:58:18

问题


I have successfully deployed a number of Azure virtual machines to an Azure Resource Group. That is, I'm using the new Azure Resource Manager deployment model to deploy the virtual machines and the related resources from a JSON template.

My problem is how to do remote PowerShell scripting from my laptop computer against these VMs. I have combed through many articles already - but they all show how to do it with the classic virtual machines in Azure. This I already know and use with success.

Now, is remote PowerShell over SSL with a certificate enabled by default on Azure VMs created with the Azure Resource Manager? How do I connect with Enter-PSSession or Invoke-Command ?


回答1:


This is for an existing machine: make sure that your VM has a public IP through the NIC settings. Next, make sure that your firewall is open to public traffic if you're going to use your laptop. This can be done with a simple netsh command:

netsh advfirewall firewall add rule name="WinRM HTTP" dir=in action=allow protocol=TCP localport=5985 profile=public

Once you have a public IP and firewall open you can enter a WinRM session with:

$username = '<admin-user>'
$pass = ConvertTo-SecureString -string '<password>' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
Enter-PSSession -ComputerName <public-IP> -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

Note: by default, WinRM over HTTP and the listener should be set up and listening on your machines. HTTPS is not enabled since it's not clear where to get the certificate. However, WinRM uses message level encryption, so it's not completely in plaintext. You can verify with:

winrm e winrm/config/listener

Which should show you the listener with something like:

Listener [Source="GPO"]
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 1.1.1.1



回答2:


You can also use the quick start template located here: https://github.com/Azure/azure-quickstart-templates/tree/4b529b00eec1a48748e2f1ea0f305c0f07c87253/undefined . If the virtual machine already exists, you can manually copy some of the files and scripts from the same location. I added instructions here: http://blog.ricardovillalobos.com/?p=1871



来源:https://stackoverflow.com/questions/33898229/remote-powershell-to-azurerm-virtual-machines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!