DEV Community

Cover image for Virtualization and Deploying Azure Virtual Machines Using Powershell

Virtualization and Deploying Azure Virtual Machines Using Powershell

Virtualization Technology and Virtual Machines

Hypervisor technology is one of the greatest inventions in the world of virtualization tech. In fact, the cloud computing platforms that we have these days like Microsoft Azure, Google Cloud Platform, AWS, other cloud providers are using virtualization technologies.

Without the technology of virtualization, we would not be able to create Virtual machines, which are the software emulations of physical computers.

A Virtual Machine includes components like virtual processors, memory, storage, and networking resources.

Virtual Machines allow us to virtualize and create a virtual computer with an operating system (also known as Virtual Machine Image) that we want. In a Virtual Machine, you have the capability to install, add software, add-ons, manage and run it like a normal computer. The only difference is that you have more benefits especially if it is hosted in the cloud!

VMs can be remote acessed using remote desktop clients, SSH, or Bastion depending on the type of image you choose.

What are Azure Virtual Machines?

Azure Virtual Machines (VM) is one of several types ofΒ on-demand, scalable computing resourcesΒ that Azure offers. Typically, you choose a VM when you need more control over the computing environments like the server infrastructure. Azure virtual machines can be used in various ways. Read more of Microsoft's Documentation for Azure Virtual Machines

Azure Virtual Machines are one of the the Azure Compute cloud services which are considered as IaaS or Infrastructure as a Service.

There are many benefits of using Azure VMs especially if your business requirements require that you or your organization have better control with your infrastructure. It is also ideal if you have existing infrastructure or old existing applications that you want to migrate to the cloud without doing spending more time in refactoring your applications, integrations and redesigning your systems architecture. It is one of the challenges many organizations experience. Cloud adoptation is not a Quick-fix. Learn more about Azure Cloud Adoptation Framework and Azure Migrate

Azure VM Manage Instances allow users to create, load-balance and auto-scale multiple Azure VMs in the cloud.

Microsoft Azure users or administrators with subscriptions can just create and provision virtual machines in the cloud for few minutes. However, there are some other ways to provision Azure VMs to Azure.

Creating and Deploying Azure Virtual Machines Programmatically

To programmatically provision Azure VMs are ideal if you want to automate the processes of the creation. Also if you want to save time and costs from starting from scratch. Of course if you want developer productivity. Think of the DRY-principle (Don't Repeat Yourself). Below are some of other ways to provision Azure Virtual Machines.

  • Azure CLI
  • Cloud Shell on Azure Portal
  • Powershell
  • Programmatically Code it REST API
  • ARM Templates

Alt Text

How to Deploy Azure Virtual Machine using Powershell

Below are some steps in deploying an Azure Virtual Machine using Powershell. The example Powershell script will deploy a VM image of Windows Server Data Center 2019.

If you want to create Linux and other images, kindly check out the learning resources links at the end of this article for some guide.

  • Prerequisites: Make sure that you have a working and active Azure Subscription
  • Create a Resource-Group in Azure to save all your resources for the Azure VMs


New-AzResourceGroup -Name myResourceGroup -Location EastUS


Enter fullscreen mode Exit fullscreen mode
  • Connect to Azure Subscription account via browser using your Azure Subscription Name


Connect-AzAccount -SubscriptionName '<<Name of Azure Subscription Account>>


Enter fullscreen mode Exit fullscreen mode
  • Verification of the right selection of Azure Subscription


Set-AzContext -SubscriptionName <<Name of Azure Subscription Account>>


Enter fullscreen mode Exit fullscreen mode
  • Create a credential to use in the VM creation NOTE: Please change the username, password with security in mind RECOMMENDED to use Azure Key Vault! ```

$username = ''
$password = ConvertTo-SecureString '' -AsPlainText -Force
$AzVMWindowsCredentials = New-Object System.Management.Automation.PSCredential ($username, $password)


* Create a Windows Virtual Machine based on desired VM Image, ex. Windows Server 2019 Datacenter 
Enter fullscreen mode Exit fullscreen mode

New-AzVM
-ResourceGroupName '' #example rg-azurevm-demo-powershell
-Name 'azvmdemo-powershell-w2019dc'
-Image 'Win2019Datacenter'
-Credential $AzVMWindowsCredentials
-OpenPorts 3389


* Get the Public IP Address to be able to remote access it via internet using RDP Client 
Enter fullscreen mode Exit fullscreen mode

Get-AzPublicIpAddress
-ResourceGroupName '' #example rg-azurevm-demo-powershell
-Name 'azvmdemo-powershell-w2019dc' | Select-Object IpAddress


* Run Powershell script via your command line 

* After running your Powershell script, access your new Azure VM using Remote Desktop via RDP Client from your computer. Login using Public IP address and the admin user credentials you set up for your VM 

*If you are just testing and doing hands-on, make sure you clean up the resources to prevent charges for the services.
Enter fullscreen mode Exit fullscreen mode

Clean up after if only testing for hands-on or demo purposes

Remove-AzResourceGroup -Name 'rg-azurevm-demo-powershell'

You will find the complete example Powershell script on one my GitHub repositories here [Create Azure VM using Powershell](https://github.com/jonahandersson/CreateAzureVMUsingPowershell/blob/main/createAzureVMWIndowsDataCenter2019.ps1#L1)

#Further Reading and Learning Resources 
* [Azure Virtual Machines in Azure](https://docs.microsoft.com/en-us/azure/virtual-machines/)
* [Microsoft Learn - Create Virtual Machine in Azure ](https://docs.microsoft.com/en-us/learn/modules/create-windows-virtual-machine-in-azure/)
* [My Talk about Azure Virtual Machines at Azure User Group Manila](https://youtu.be/Ym1hzEfgoWE)

If you like this article, please re-share the knowledge! 
Feel free to leave feedback and ask questions.  

Follow me [Twitter](https://twitter.com/cjkodare), connect via my [LinkedIn](https://www.linkedin.com/in/jonahandersson/) and visit my [website](https://jonahandersson.tech) 
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
dasanasak profile image
Dosa Nasyak

Microsoft's prices are insane. It is much more profitable for me to rent virtual machines from G-core labs.

Collapse
 
lola_tray profile image
Lola Tray Rosendo

Their support team is available 24/7, and not through a contact form, but can be contacted directly, which cannot be done with Azure if I clearly understand.