CREATING A VIRTUAL MACHINE ON AZURE USING POWERSHELL

aizeon

Emmanuel Oluajo

Posted on June 25, 2024

CREATING A VIRTUAL MACHINE ON AZURE USING POWERSHELL

There are several routes that can be taken to achieve the deployment of resources and services on Azure. Whether it be through clicking and selecting resources directly on Azure portal or using scripting tools like PowerShell, Windows PowerShell or Command Prompt.
For today, I will be using the PowerShell application to create a virtual machine on Azure.

PREREQUISITE

  • Working computer
  • Internet connection
  • Microsoft Azure account + active subscription
  • PowerShell application

PROCEDURE

INSTALL AZURE POWERSHELL MODULE

Regardless of which OS runs on your computer, click on this link (https://learn.microsoft.com/en-us/powershell/azure/?view=azps-12.0.0) to get guidelines on how to install or update the latest Azure PowerShell module.

Image description

CONNECT THE POWERSHELL MODULE TO AZURE

To achieve this, type in this command Connect-AzAccount in the PowerShell interface.
You either have a sign-in webpage or a sign-in pop-up window appearing on your screen after entering that command.
Select the Azure account you want to log in with and click on “Continue” as shown below.

Image description

A list of subscriptions you have available will be generated. Select the one you require and we will be on our way.

Image description

CREATE A RESOURCE GROUP

A virtual machine is a resource hence, it needs a resource group to house it.
Create a resource group by entering a command in PowerShell following the format below:
New-AzResourceGroup -Name "ResourceGroupName" -Location "Region Name"
NB: The quoted words are to be customised to suit your requirements.
A message informing users of the success will be displayed on the following line.

Image description

The newly created resource group can also be located on the Azure portal for further verification.

Image description

CREATE A VIRTUAL MACHINE

In a manner similar to how a resource group was created, a virtual machine can also be created but in this case, providing more specifications as we would on the Azure portal when creating a VM.
Enter a command in PowerShell following the format below:
New-AzVm -ResourceGroupName "ResourceGroupName" -Name "VMName" -Location "Region Name" -VirtualNetworkName "VNetName" -SubnetName "SubnetName" -SecurityGroupName "NSGName" -PublicIpAddressName "PublicIPName" -OpenPorts 80,3389 -Size "VMSize" -Credential (New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password0!" -AsPlainText -Force))) -Image "OperatingSystemName"
NB: The quoted words are to be customised to suit your requirements.
A message of this format will appear: Creating Azure resources [4% ]

Image description

When completed, a list will be generated containing the resource group name, VM name and ID, location, provisioning state, time created and many more.

Image description

As done earlier, the VM can be located on the Azure portal for further verification.
Open the VM resource on the portal.

Image description

On the VM page, click on “Settings” and then, “Advisor recommendations” to see if any part of the VM and other resources isn’t optimal.

Image description

Image description

Image description

💖 💪 🙅 🚩
aizeon
Emmanuel Oluajo

Posted on June 25, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related