Hi Everyone. Today I will link couple of the previously discussed subjects together:
· Naming Convention
· Azure Portal Limitations
If you followed the steps from last week, you should have a Resource Group and a Vnet similar to the following:
Please go ahead and create a VM using the portal. It's not hard… Just click on the + sign at the top left and follow the instructions.
For the demonstration today I decided to go use Managed Disks, and I removed the Public IP Address, Network Security Group & Monitoring from my VM:
After few happy clicks in the portal, I have a new VM and my Resource Group looks like that:
I am not happy! I spent many hours of planning the perfect naming convention, but I can't use it. If I will continue building VM's using the portal - it's going to get ugly and very difficult to manage.
I would like it to look like that:
I used PowerShell to create the assets with the above names. There are other tools (CLI, ARM Templates) that will deliver the same result.
So, here is the script:
$ResourseGroupName = "RG-Blog-Mel"
$Location = "Australia SouthEast"
$VnetName = "BlogVnet"
$Vnet = Get-AzureRmVirtualNetwork -ResourceGroupName "RG-Blog-Mel" -Name $vnetName
$Subnet =Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name BackEnd
$VMSize = "Standard_DS1_V2"
$Skus = "2012-R2-Datacenter"
$VMName = "BlogVM1"
$nicname = $VMName + "-NIC"
$Interface = New-AzureRmNetworkInterface -Name $nicname -ResourceGroupName $ResourseGroupName -Location $location -Subnet $Subnet
$ComputerName = $VMName
$timeZone = "AUS Eastern Standard Time"
$OSDiskName = $VMName + "-OSdisk"
$Credential = Get-Credential
$VirtualMachine = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Window -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate -TimeZone $timeZone
$VirtualMachine = Set-AzureRmVMSourceImage -VM $VirtualMachine -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus $Skus -Version "latest"
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $OSDiskName -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $ResourseGroupName -Location $location -VM $VirtualMachine -DisableBginfoExtension
This is a VERY basic PowerShell script. I pass all the variables manually into the script. The only input required from the operator is the $Credential = Get-Credential command.
This script cannot scale for multiple VM's with different configuration and operating systems.
Next week, will discuss how we can make this script more generic, so we can scale and use it to build multiple machines with different configurations.
Have a great weekend!