In a previous post I gave some steps to troubleshoot connecting to your Azure VM via Powershell.
You can view it here
Today I want to describe how you can make it nice and simple to connect via Powershell to your VM.
There are a couple of steps to complete before being allowed to connect, they are:
1. Deploy the VM
This one is pretty simple and there are plenty of examples on deploying a VM with PS in Azure. I have also written a script to help located here
Or you can deploy via the management console if you want… it doesn't really matter as long as you record the Cloud Service to which you deployed and the Virtual Machine Name
2. Download Certificate
Using the Virtual Machine name and the cloud service from an ELEVATED POWERSHELL prompt
Use the following code (this is thanks to michael washam on TechNet gallery)
The certificate the is deployed along with the VM can be retrieved and stored on the local computer so you can trust it and make a secure Powershell connection using SSL
3. Determine Public Port
Next thing we need to do is determine the public port and the public name this is published under. The Name is pretty simple. if you have deployed it under an existing Cloud Service it will be that name else it will be the name of the cloud service you have published the VM under. If you have completely forgotten you can retrieve it using the Get-AzureVM cmdlet as shown below
you can drop the http:// and the trailing / we are only interested in the fqdn part
The next problem is determining the port! if you are lucky to deploy the first service into your cloud you will get the default port of 5986 for HTTPS WinRM but most of the time it will be some random port.
Using the following Powershell will retrieve the port for you and store it in a variable! Cool now we can use it to connect!
4. Connect To System
At this point we can connect! You have the choice of using Enter-Pssession or New-Pssession and you need to pass credentials.. the follow piece of code will help you connect to a PSSession we are going to store in a variable so we can use it later in the cmdlet invoke-command
One thing to note is the $cred … in my example I have already stored the credentials, however if you want to capture it each time you can replace $cred with (Get-Credential) and it will prompt you during execution for the connection credentials…
‘Hope it helps!