DEV Community

loading...
Cover image for Virtualization and Deploying Azure Virtual Machines Using Powershell

Virtualization and Deploying Azure Virtual Machines Using Powershell

jonahandersson profile image Jonah Andersson 👩🏻‍💻SE ・4 min read

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 = '<Your Desired VM admin Name here>' 
$password = ConvertTo-SecureString '<Your Secure Password Here>' -AsPlainText -Force
$AzVMWindowsCredentials = New-Object System.Management.Automation.PSCredential ($username, $password)
Enter fullscreen mode Exit fullscreen mode
  • Create a Windows Virtual Machine based on desired VM Image, ex. Windows Server 2019 Datacenter
New-AzVM 
    -ResourceGroupName '<Your Resource Group Name here>' #example rg-azurevm-demo-powershell
    -Name 'azvmdemo-powershell-w2019dc' 
    -Image 'Win2019Datacenter' 
    -Credential $AzVMWindowsCredentials 
    -OpenPorts 3389

Enter fullscreen mode Exit fullscreen mode
  • Get the Public IP Address to be able to remote access it via internet using RDP Client
Get-AzPublicIpAddress 
    -ResourceGroupName '<Your Resource Group Name here>' #example rg-azurevm-demo-powershell
    -Name 'azvmdemo-powershell-w2019dc' | Select-Object IpAddress
Enter fullscreen mode Exit fullscreen mode
  • 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.

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

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

Enter fullscreen mode Exit fullscreen mode

You will find the complete example Powershell script on one my GitHub repositories here Create Azure VM using Powershell

Further Reading and Learning Resources

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

Follow me Twitter, connect via my LinkedIn and visit my website

Discussion (0)

pic
Editor guide