Virtualization and Deploying Azure Virtual Machines Using Powershell
Tags: azurevm, azurevirtualmachine, powershell, virtualization
Published: 2021-02-27 16:34:38

#Virtualization Technology and Virtual Machines

[Hypervisor technology](https://en.wikipedia.org/wiki/Hypervisor) 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](https://docs.microsoft.com/en-us/azure/virtual-machines/windows/overview#:~:text=Azure%20Virtual%20Machines%20(VM)%20is,computing%20resources%20that%20Azure%20offers.&text=Development%20and%20test%20%E2%80%93%20Azure%20VMs,code%20and%20test%20an%20application)

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](https://azure.microsoft.com/en-us/cloud-adoption-framework/) and [Azure Migrate](https://azure.microsoft.com/en-us/services/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bwxy0h9yfoe85uyuksvd.png)

#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
```
* Connect to Azure Subscription account via browser using your Azure Subscription Name 

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

* Verification of the right selection of Azure Subscription 

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

* 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)
```

* 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

```

* 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
```

* 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'

```
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)