In this article I will show you how you can create Azure VM with Azure Powershell Script that will help you to provision your VM in just few Sec.

Always remember this custom Azure PowerShell script will create following resources-

  • Resource Group
  • Virtual Network with one Subnet
  • Network Security Group with NSG rule that will allow access to VMs
  • Virtual Machine with CentOS operating system

Let’s start to understand each steps first before jumping to collect master script 🙂

Define variables for networking Configuration

$ResourceGroup = ""
$Location = ""
$vNetName = ""
$AddressSpace = "" # Format 10.10.0.0/16
$SubnetIPRange = "" # Format 10.10.1.0/24
$SubnetName = ""
$nsgName = ""
$StorageAccount = "" # Name must be unique. 
# Note- Storage account Name availability can be check using PowerShell command, Get-AzureRmStorageAccountNameAvailability -Name "Give your name" 

Create Resource Group and Storage Account for diagnostic

New-AzureRMResourceGroup -Name $ResourceGroup -Location $Location
New-AzureRMStorageAccount -Name $StorageAccount -ResourceGroupName $ResourceGroup -Location $Location -SkuName Standard_LRS

Create Virtual Network and Subnet

$vNetwork = New-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName -AddressPrefix $AddressSpace -Location $location
 Add-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNetwork -AddressPrefix $SubnetIPRange
 Set-AzureRmVirtualNetwork -VirtualNetwork $vNetwork

Create Network Security Group

$nsgRuleVMAccess = New-AzureRMNetworkSecurityRuleConfig -Name 'allow-vm-access' -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22,3389 -Access Allow
 New-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $location -Name $nsgName -SecurityRules $nsgRuleVMAccess

Define Variables needed for Virtual Machine

$vNet = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName
 $Subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
 $nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NsgName
 $vmName = "DemoVM"
 $pubName = "DemoPIP"
 $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-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $ResourceGroup -Location $location -AllocationMethod Static
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroup -Location $location -SubnetId $Subnet.Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id

Set VM Configuration

$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id

Set VM operating system parameters

Set-AzureRmVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential $adminCreds

Set boot diagnostic storage account

Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName $ResourceGroup -VM $vmConfig -StorageAccountName $StorageAccount

Set virtual machine source image

Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName $pubName -Offer $offerName -Skus $skuName -Version 'latest'

Set OsDisk configuration

Set-AzureRmVMOSDisk -VM $vmConfig -Name $osDiskName -DiskSizeInGB $osDiskSize -StorageAccountType $osDiskType -CreateOption fromImage

Create the VM

New-AzureRmVM -ResourceGroupName $ResourceGroup -Location $location -VM $vmConfig

Master Script To Create VM-

Including all steps here is the master script that will create a Azure Virtual Machine including the vNet, NSG, Storage Account and Public IP that will allow VM to access at port 22 and 3389.

 # Define variables for networking part
  $ResourceGroup  = ""
  $Location       = ""
  $vNetName       = ""
  $AddressSpace   = "" # Format 10.10.0.0/16
  $SubnetIPRange  = "" # Format 10.10.1.0/24
  $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-AzureRmResourceGroup -Name $ResourceGroup -Location $Location
  New-AzureRmStorageAccount -Name $StorageAccount -ResourceGroupName $ResourceGroup -Location $Location -SkuName Standard_LRS
 # Create Virtual Network and Subnet
  $vNetwork = New-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName -AddressPrefix $AddressSpace -Location $location
  Add-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNetwork -AddressPrefix $SubnetIPRange
  Set-AzureRmVirtualNetwork -VirtualNetwork $vNetwork
 # Create Network Security Group
  $nsgRuleVMAccess = New-AzureRmNetworkSecurityRuleConfig -Name 'allow-vm-access' -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22,3389 -Access Allow
  New-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Location $location -Name $nsgName -SecurityRules $nsgRuleVMAccess
 # Define Variables needed for Virtual Machine
  $vNet       = Get-AzureRmVirtualNetwork -ResourceGroupName $ResourceGroup -Name $vNetName
  $Subnet     = Get-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vNet
  $nsg        = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NsgName
  $vmName     = "Demo-VM"
  $pubName    = "DemoPiP"
  $offerName    = "centOS"
  $skuName    = "7.5"
  $vmSize     = "Standard_Ds1_v2"
  $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-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $ResourceGroup -Location $location -AllocationMethod Static 
  $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroup -Location $location -SubnetId $Subnet.Id -PublicIpAddressId $pip.Id -NetworkSecurityGroupId $nsg.Id
 # Set VM Configuration
  $vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
  Add-AzureRmVMNetworkInterface -VM $vmConfig -Id $nic.Id
 # Set VM operating system parameters
  Set-AzureRmVMOperatingSystem -VM $vmConfig -Linux -ComputerName $vmName -Credential $adminCreds
 # Set boot diagnostic storage account
  Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName $ResourceGroup -VM $vmConfig -StorageAccountName $StorageAccount
 # Set virtual machine source image
  Set-AzureRmVMSourceImage -VM $vmConfig -PublisherName $pubName -Offer $offerName -Skus $skuName -Version 'latest'
 # Set OsDisk configuration
  Set-AzureRmVMOSDisk -VM $vmConfig -Name $osDiskName -DiskSizeInGB $osDiskSize -StorageAccountType $osDiskType -CreateOption fromImage
 # Create the VM
  New-AzureRmVM -ResourceGroupName $ResourceGroup -Location $location -VM $vmConfig