
Recently I have been taking an online training course in preparation to take the Microsoft 70-532 certification exam. My studies have made me realize how easy to use and fun playing in the cloud can be. You can create a virtual machine, install some stuff, do some stuff and then delete it all in no time. To do something similar on my laptop or an on-premise VM could take a few days. While my company is not looking to move to Azure anytime soon, this is a skill I would like to put on my resume as well as a technology that I would like to start taking advantage of to make testing and automation easier.
When I run across cool demos or blogs I try to execute all the steps in order to learn, although it can be hard to get Azure credits to do this testing and investigating. With my A Cloud Guru course I was able to create a free account with Azure and get $200 in credits that lasted a month. Too bad I left a Redis device running that the class covered about half way thru that ate up 75% of those credits. I also have an MSDN Visual Studio Professional Subscription through my work where I get $50 in credit a month. My Azure time is limited by when the 17th of the month rolls around and my MSDN subscription resets. Yay!!!
On Tuesday when I saw a tweet by the famous Bob Ward ( t ) advertising how the SQL Team had just released a self-paced tutorial to GitHub on how to install SQL Server on Linux and use Containers. For me that was a trifecta of things I have been trying to fit in to my schedule and learn!!! And the fact I just got my shiny new $50 Azure credits, it was go time.
I spent the bulk of my day Wednesday going through the Prelab steps outlined in the lab. I was extremely impressed by this lab and how every step was correct and accurate down to the letter. Then the more I thought about it, the steps are built around using an Azure Virtual Machine. With this you get a common machine, framework and steps to build around. You do not have to worry about the users’ local settings or scenario. You are starting from the exact same point of reference every time. So that was fun to connect via SSH to a Linux machine and install SQL Server 2017 and Docker from the command line. While I know it was easy because someone was telling me what to type, it was still fun to see how the other side (Linux People) live.
Today I was in an adventurous mood to try something new. I had been wanting to put together a PowerShell script that would deploy an Azure Virtual Machine. I started down the path a couple time and got stuck so I lost interest. I thought this was the perfect opportunity to get over the hurdle and combine the Prelab steps in this lab with doing those steps with PowerShell. So below you will find my first go at building an Azure Virtual Machine using PowerShell to replace the manual steps in the Prelab process. Not that there was anything wrong with those steps, I just want to try and use a tool that I have been working to learn and use on a day to day basis. Wish me luck.
Deploy an Azure Virtual Machine running Linux
- Login to Azure
1 2 3 |
## Azure Account ## ## Connect PowerShell to your Azure account Connect-AzureRmAccount |

You will get this prompt to login to your Azure Account
2. Next you will want to set some configuration settings that will be needed to proceed
1 2 3 4 5 6 7 8 9 10 |
## Azure Configurations $AzureResourceGroup = "LinuxLab" $AzurePublisher = 'RedHat' $AzureLocation = Get-AzureRmLocation | Out-GridView -PassThru #Location - eastus $AzureOffer = Get-AzureRmVMImageOffer -Location $AzureLocation.Location -PublisherName $AzurePublisher | Out-GridView -PassThru #Offer = RHEL $AzureSku = Get-AzureRmVMImageSku -Location $AzureLocation.Location -PublisherName $AzurePublisher -Offer $AzureOffer.Offer | Out-GridView -PassThru #Sku - 7-RAW Get-AzureRmVmImage -Location $AzureLocation.Location -PublisherName $AzurePublisher -Offer $AzureOffer.Offer -Skus $AzureSku.Skus |

Here we Choose a Location for our resources

Now we choose the Offer

Finally we select the Sku
3. Now that we have our OS information needed for the Virtual Machine we can set our variables for Storage, Network and VM configurations settings that will be used to deploy the virtual machine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## Azure Storage Configurations $AzureVmStorageAccountName = "linuxlabstoragegb" $AzureVmStorageType = "Standard_LRS" ## Azure Network Configuration $AzureVmInterfaceName = "LinuxLabInterface" $AzureVmSubnetName = "LinuxLabSubnet" $AzureVmNetName = "LinuxLabVNet" $AzureVmAddressPrefix = "10.0.0.0/16" $AzureVmSubnetAddressPrefix = "10.0.0.0/24" ## Azure Linux VM Configurations $AzureVmName = "rhLinux75" $AzureVmSize = Get-AzureRmVMSize -Location $AzureLocation.Location | Out-GridView -PassThru #Size - Standard_B4ms $AzureOsDiskName = $AzureVmName + "OSDisk" |
4. Now with all our variables set with the configurations we want it is time to build the resources in Azure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
## Azure Build ## ## Resource Group New-AzureRmResourceGroup -Name $AzureResourceGroup -Location $AzureLocation.Location ## Storage $AzureVmStorageAccount = New-AzureRmStorageAccount -ResourceGroupName $AzureResourceGroup -Name $AzureVmStorageAccountName -Type $AzureVmStorageType -Location $AzureLocation.Location ## Network $AzureVmPublicIp = New-AzureRmPublicIpAddress -Name $AzureVmInterfaceName -ResourceGroupName $AzureResourceGroup -Location $AzureLocation.Location -AllocationMethod Dynamic $AzureVmSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig -Name $AzureVmSubnetName -AddressPrefix $AzureVmSubnetAddressPrefix $AzureVmVNetConfig = New-AzureRmVirtualNetwork -Name $AzureVmNetName -ResourceGroupName $AzureResourceGroup -Location $AzureLocation.Location -AddressPrefix $AzureVmAddressPrefix -Subnet $AzureVmSubnetConfig $AzureVmInterfaceConfig = New-AzureRmNetworkInterface -Name $AzureVmInterfaceName -ResourceGroupName $AzureResourceGroup -Location $AzureLocation.Location -SubnetId $AzureVmVNetConfig.Subnets[0].Id -PublicIpAddressId $AzureVmPublicIp.Id $AzureVmNetworkRule1 = New-AzureRmNetworkSecurityRuleConfig -Name SSH -Protocol Tcp -SourcePortRange * -DestinationPortRange 22 -SourceAddressPrefix * -DestinationAddressPrefix * -Access Allow -Priority 300 -Direction Inbound $AzureVmNetworkRule2 = New-AzureRmNetworkSecurityRuleConfig -Name MS_SQL -Protocol Tcp -SourcePortRange * -DestinationPortRange 1433 -SourceAddressPrefix * -DestinationAddressPrefix * -Access Allow -Priority 330 -Direction Inbound $AzureVmNetworkSecurityGroup = New-AzureRmNetworkSecurityGroup -Name "networkSecurityGroups_$($AzureVMName)_nsg_name" -ResourceGroupName $AzureResourceGroup -Location $AzureLocation.Location -SecurityRules $AzureVmNetworkRule1, $AzureVmNetworkRule2 $AzureVmInterfaceConfig = Get-AzureRmNetworkInterface -ResourceGroupName $AzureResourceGroup -Name $AzureVmInterfaceName $AzureVmNetworkSecurityGroup = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $AzureResourceGroup -Name "networkSecurityGroups_$($AzureVMName)_nsg_name" $AzureVmInterfaceConfig.NetworkSecurityGroup = $AzureVmNetworkSecurityGroup $AzureVmInterfaceConfig | Set-AzureRmNetworkInterface # Virtual Machine $AzureVmUsername = "user-gbargsley" $AzureVmPassword = ConvertTo-SecureString "Garry!23Garry!23" -AsPlainText -Force $AzureVmCredential = New-Object System.Management.Automation.PSCredential ($AzureVmUsername, $AzureVmPassword) $AzureVmConfig = New-AzureRmVMConfig -VMName $AzureVmName -VMSize $AzureVmSize.Name $AzureVmConfig = Set-AzureRmVMOperatingSystem -VM $AzureVmConfig -ComputerName $AzureVmName -Linux -Credential $AzureVmCredential $AzureVmConfig = Set-AzureRmVMSourceImage -VM $AzureVmConfig -PublisherName $AzurePublisher -Offer $AzureOffer.Offer -Skus $AzureSku.Skus -Version "latest" $AzureVmConfig = Add-AzureRmVMNetworkInterface -VM $AzureVmConfig -Id $AzureVmInterfaceConfig.Id $AzureVmOsDiskUri = $AzureVmStorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $AzureOsDiskName + ".vhd" $AzureVmConfig = Set-AzureRmVMOSDisk -VM $AzureVmConfig -Name $AzureOsDiskName -VhdUri $AzureVmOsDiskUri -CreateOption FromImage |

All the resources!!!
5. Now with all the supporting resources configured and showing in the Portal, we can run the final command that will do all the work of creating the Linux Virtual Machine and starting it in our Azure Portal.
1 2 |
## Create the VM in Azure ## New-AzureRmVM -ResourceGroupName $AzureResourceGroup -Location $AzureLocation.Location -VM $AzureVmConfig |

Ta Da !!!
6. Now only six minutes later we have a new Azure Resource Group with all the resources for our new Linux Virtual Machine and I can prove it.
Conclusion
With the creation of the Virutal Machine, I can now work to complete the remainder of the demo lab and learn some Linux SQL and Container goodness!!!
Happy reading and I hope this helps you in your learning journey. And as always any feedback or more efficient methods are welcome.