Creating a Virtual Machine in Azure with Powershell

Azure Virtual Machines (VMs) are a popular service offered by Microsoft Azure that allow users to create, deploy, and manage virtual machines on Azure. In this article, we will discuss how to create an Azure VM using REST API with PowerShell, along with code examples.

Prerequisites:

Before we get started, make sure you have the following prerequisites:

  1. An Azure account with the necessary permissions to create virtual machines.
  2. PowerShell installed on your local machine.
  3. Azure PowerShell module installed on your local machine.
  4. Basic knowledge of PowerShell scripting and REST API.

Steps to Create an Azure VM using REST API with PowerShell:

  1. Create an Azure Active Directory (AAD) application and service principal.

To create an AAD application and service principal, follow the instructions provided in the Microsoft documentation: https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal.

  1. Create an Azure resource group.

To create an Azure resource group, run the following PowerShell command:

New-AzResourceGroup -Name "ResourceGroupName" -Location "EastUS"

Replace “ResourceGroupName” and “EastUS” with your preferred resource group name and location.

  1. Create an Azure virtual network.

To create an Azure virtual network, run the following PowerShell command:

$subnet = New-AzVirtualNetworkSubnetConfig -Name "SubnetName" -AddressPrefix "10.0.0.0/24"
New-AzVirtualNetwork -Name "VNetName" -ResourceGroupName "ResourceGroupName" -Location "EastUS" -AddressPrefix "10.0.0.0/16" -Subnet $subnet

Replace “SubnetName”, “VNetName”, “ResourceGroupName”, and “EastUS” with your preferred values.

  1. Create an Azure virtual machine.

To create an Azure virtual machine, run the following PowerShell command:

$vmName = "VMName"
$vmSize = "Standard_DS1_v2"
$vmImagePublisher = "MicrosoftWindowsServer"
$vmImageOffer = "WindowsServer"
$vmImageSku = "2019-Datacenter"
$adminUsername = "AdminUsername"
$adminPassword = "AdminPassword"

$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize
$vmConfig = Set-AzVMOperatingSystem -VM $vmConfig -Windows -ComputerName $vmName -Credential (New-Object System.Management.Automation.PSCredential ($adminUsername, (ConvertTo-SecureString $adminPassword -AsPlainText -Force)))
$vmConfig = Set-AzVMSourceImage -VM $vmConfig -PublisherName $vmImagePublisher -Offer $vmImageOffer -Skus $vmImageSku -Version latest
$vmConfig = Add-AzVMNetworkInterface -VM $vmConfig -Id <NICResourceId>
$vmConfig = Set-AzVMOSDisk -VM $vmConfig -Name $vmName -CreateOption fromImage -Caching ReadOnly -StorageAccountType Standard_LRS

New-AzVM -ResourceGroupName "ResourceGroupName" -Location "EastUS" -VM $vmConfig

Replace “VMName”, “Standard_DS1_v2”, “MicrosoftWindowsServer”, “WindowsServer”, “2019-Datacenter”, “AdminUsername”, “AdminPassword”, “ResourceGroupName”, and “EastUS” with your preferred values.

  1. Verify the Azure VM creation.

To verify the Azure VM creation, run the following PowerShell command:

Get-AzVM -ResourceGroupName "ResourceGroupName" -Name "VMName"

Replace “VMName” with the machine name you specified

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *