This post is basically pretty old, but because script is updated, we can announce that post is new 🙂

In one of previous posts, you have learned how to create Azure VM using Azure Portal. Now, I will show you how you can do that using Azure PowerShell.

Following script will:

  • Create Resource Group
  • Create Virtual Network with one Subnet
  • Create Network Security Group with rule that will allow access to VMs
  • Create Virtual Machine with centOS 7.5 operating system
# Define variables for networking part
$ResourceGroup  = ""
$Location       = ""
$vNetName       = ""
$AddressSpace   = "" # Format
$SubnetIPRange  = "" # Format
$SubnetName     = ""
$nsgName        = ""
$StorageAccount = "" # Name must be unique. Name availability can be check using PowerShell command Get-AzStorageAccountNameAvailability -Name ""

# Create Resource Groups and Storage Account for diagnostic
New-AzResourceGroup -Name $ResourceGroup -Location $Location
New-AzStorageAccount -Name $StorageAccount -ResourceGroupName $ResourceGroup -Location $Location -SkuName Standard_LRS

# Create Virtual Network and Subnet
$vNetwork = New-AzVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName -AddressPrefix $AddressSpace -Location $location
Add-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNetwork -AddressPrefix $SubnetIPRange
Set-AzVirtualNetwork -VirtualNetwork $vNetwork

# Create Network Security Group
$nsgRuleVMAccess = New-AzNetworkSecurityRuleConfig -Name 'allow-vm-access' -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22,3389 -Access Allow
New-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $location -Name $nsgName -SecurityRules $nsgRuleVMAccess

# Define Variables needed for Virtual Machine
$vNet       = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName
$Subnet     = Get-AzVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
$nsg        = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NsgName
$vmName 	= "centOS-01"
$pubName	= "OpenLogic"
$offerName	= "centOS"
$skuName	= "7.5"
$vmSize 	= "Standard_B1s"
$pipName    = "$vmName-pip" 
$nicName    = "$vmName-nic"
$osDiskName = "$vmName-OsDisk"
$osDiskSize = "30"
$osDiskType = "Premium_LRS"

# Create Admin Credentials
$adminUsername = Read-Host 'Admin username'
$adminPassword = Read-Host -AsSecureString 'Admin password with least 12 characters'
$adminCreds    = New-Object PSCredential $adminUsername, $adminPassword

# Create a public IP and NIC
$pip = New-AzPublicIpAddress -Name $pipName -ResourceGroupName $ResourceGroup -Location $location -AllocationMethod Static 
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroup -Location $location -SubnetId $Subnet.Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

# Set VM Configuration
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize $vmSize
Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id

# Set VM operating system parameters
Set-AzVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential $adminCreds

# Set boot diagnostic storage account
Set-AzVMBootDiagnostics -Enable -ResourceGroupName $ResourceGroup -VM $vmConfig -StorageAccountName $StorageAccount

# Set virtual machine source image
Set-AzVMSourceImage -VM $vmConfig -PublisherName $pubName -Offer $offerName -Skus $skuName -Version 'latest'

# Set OsDisk configuration
Set-AzVMOSDisk -VM $vmConfig -Name $osDiskName -DiskSizeInGB $osDiskSize -StorageAccountType $osDiskType -CreateOption fromImage

# Create the VM
New-AzVM -ResourceGroupName $ResourceGroup -Location $location -VM $vmConfig


This way is little bit complicated, especially if you are now familiar with PowerShell, but you really need to start to use PowerShell.

Trust me 🙂



Please follow and like us:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.