In this blog post I will deploy virtual servers within the Azure Portal using Powershell via Azure Cloudshell.
1) Login to the Azure Portal portal.azure.com
2) Click the Cloud Shell icon found towards the top of the portal
3) Click Powershell
4) Click Create Storage. If you want to configure custom settings, click Show Advanced Settings
5) and we’re connected
6) Before creating a Virtual Machine, I will create a resource group to where I will deploy my new VM. My new resource group is named CloudBuildPSRG (PS for PowerShell and RG for Resource Group). My location is UKSouth. You could create this resource group as part of the VM Build commands further down this blog post but for the purpose of this demo, I will create the resource group first.
New-AzResourceGroup -Name CloudBuildPSRG -Location UKSouth
7) If I visit the resource group area within the Azure Portal, here is my newly created resource group
8) We don’t want to only view the new resource group via the portal, let’s take a look at the resource group via PowerShell. Here is the code to display your resource groups
And here is the resource group
9) Let’s move onto creating a VM within this new resource group
Before running the below commands, i’ll explain what each line of code will do
New-AzVm ` -ResourceGroupName "CloudBuildPSRG" ` -Name "CloudBuildPSVM" ` -Location "UK South" ` -VirtualNetworkName "CloudBuild-PSVNET" ` -SubnetName "subnet1" ` -SecurityGroupName "CBNetworkSecurityGroup" ` -PublicIpAddressName "GBPublicIpAddress" ` -OpenPorts 80,3389
-ResourceGroupName “CloudBuildPSRG” – I will use an existing Resource Group that I created in this blog post earlier. In the event the resource group does not exist, a new resource group will be created.
-Name “CloudBuildPSVM” – This is the name of the VM
-Location “UK South” – The VM will be built in region UK South
-VirtualNetworkName “CloudBuild-PSVNET” – I am creating a new VNET but you could also use an existing VNET name if you have already created one
-SubnetName “subnet1” – A new subnet will be created named subnet1. Again you could use an existing by specifying the name.
-SecurityGroupName – NSG name for the VM (Network Security Group)
-PublicIpAddressName “GBPublicIpAddress” – For the purpose of this lab, I will be creating a public IP address. This is something you don’t want to do for a production server. You could use Azure Bastion to connect to a VM from the portal, or connect to the VM from your internal network over VPN.
-OpenPorts 80,3389 – Opening ports within the NSG (Network Security Group) to allow access to the web service and Remote Desktop access. My next blog post will include the installation of IIS via powershell and testing access externally.
10) Let’s continue with running the script. After triggering the script, you’re prompted to create a new local admin username and password for the VM.
and the machine build is in progress
VM build successful
11) Let’s check the status of the VM
get-azvm -name CloudBuildPSVM
12) Let’s check the Azure Portal. There it is. The VM has been deployed in my existing resource group CloudBuildPSRG
13) I’ll now obtain the Public IP address of the VM so I can connect to it. (Note that this is a demo. In a production environment you don’t want to allow RDP access externally). The Public IP could also be obtained from the Azure Portal, but as we’re doing everything within PowerShell, let’s continue with Powershell.
Here is the command I will run to obtain the public IP address of my newly created VM
Get-AzPublicIpAddress -Name GBPublicIpAddress -ResourceGroupName CloudBuildPSRG | Select IPAddress
14) You can now connect to your server
This process creates a Windows 2016 Datacenter server, but what if you want to use a different image available within the Microsoft Azure Marketplace?
Let’s continue with building another VM but this time specifying what image we want to use.
15) Type Get-AzVMImageOffer -Location “UK South” -PublisherName “MicrosoftWindowsServer”
A Marketplace image in Azure has the following attributes:
- Publisher: The organisation that created the image. Examples: Canonical, MicrosoftWindowsServer
- Offer: The name of a group of related images created by a publisher. Examples: UbuntuServer, WindowsServer
- SKU: An instance of an offer, such as a major release of a distribution. Examples: 18.04-LTS, 2019-Datacenter
- Version: The version number of an image SKU.
MicrosoftWindowsServer is a VM publisher name. If you want to view all VM image publishers available within the market place in the UK South region, the command is as follows: Get-AzVMImagePublisher -location “UK South”
16) Here are the results from step 15. The below results show that I have a number of Microsoft Server authors available in the UK South region. I will be using WindowsServer
17) We now dig deeper and find out what images are available within the WindowsServer Publisher selection
Get-AzVMImageSku -Location “UK South” -PublisherName “MicrosoftWindowsServer” -Offer “WindowsServer”
and after running the command below, we have a selection to choose from:
18) Let’s deploy a 2012 R2 Datacenter server
19) Here is what the script look like this time.
-ResourceGroupName “CloudBuildPSRG” `
-Name “CloudBuildPSVM3” `
-Location “uksouth” `
-VirtualNetworkName “CloudBuild-PSVNET” `
-SubnetName “subnet1” `
-SecurityGroupName “CBNetworkSecurityGroup3” `
-PublicIpAddressName “GBPublicIpAddress3” `
-ImageName “MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest” `
-OpenPorts 80,3389 `
AsJob allows the command to run in the background allowing you to use PowerShell for other tasks and not have to wait for the script to complete, as you’ll see from the results below.
latest – is a command which requests for the latest image available
After running the script above, as you can see from the screenshot below the output is different because of the additional command -AsJob. The job is now running in the background which means I don’t have to wait for PowerShell to complete the process.
20) And we have successfully deployed a Windows 2012 R2 Datacenter server