Using PowerShell to create VMs in Azure

There are several methods to deploy virtual machines on Azure. One of my favorite is using PowerShell. Using PowerShell to deploy VMs, gives you the ability to automate the process and be more efficient.

Usually I like to create the master PowerShell template where the variables will do all the work. By using this methodology, I found that I can use that PS code over and over, even in occasions that need some additional options (example: add data disks).

To create an Azure Resource Manager virtual machine by using Azure PowerShell, perform the following steps:

  1. Open the Azure PowerShell command prompt.
  2. Sign in to Azure by typing the following cmdlet:
    Login-AzureRmAccount
  3. Select of Azure subscriptions associated with your account by running the following cmdlet:
    Get-AzureRmSubscription
  4. Set your subscription by typing the following cmdlet, and then pressing Enter:
    Select-AzureRmSubscription -SubscriptionName “Subscription Name”
  5. Use the following code block to create the virtual machine, storage account, and associated network objects. In the code, you must replace the variables with the appropriate values from your environment.

#Example values

$StorageAccountName = “STORAGE_NAME”
$StorageType = “Standard_LRS”
$Location = “West US”
$ResourceGroupName = “Resrouce_Group_Name”
$VnetName = “VNET_Name”
$VnetIP = “10.0.0.0/16”
$SubnetName = “Subnet_Name”
$SubnetIP = “10.0.1.0/24”
$PublicIP = ($VMName + “-IPP”)
$VMName = “MDNOGAVM01”
$VMSize = “Standard_A1”
$VMNic = “$VMName-Nic”
$OSSku = “2012-R2-Datacenter”

#PowerShell Script

New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location
$storageAcc = New-AzureRmStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -Type $StorageType -Location $Location
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetIP
$vnet = New-AzureRmVirtualNetwork -Name $VnetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VnetIP -Subnet $singleSubnet
$pip = New-AzureRmPublicIpAddress -Name $PublicIP -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $VMNic -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$cred = Get-Credential -Message “Type the name and password of the local administrator account.”
$vm = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $VMName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus $OSSku -Version “latest”
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + “vhds/$VMName.vhd”
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $VMName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureRmVM -ResourceGroupName $ResourceGroupName -Location $Location -VM $vm 

Written by Marcos Nogueira

Marcos Nogueira

With more than 18 years experience in Datacenter Architectures, Marcos Nogueira is currently working as a Principal Cloud Solution Architect. He is an expert in Private and Hybrid Cloud, with a focus on Microsoft Azure, Virtualization and System Center. He has worked in several industries, including Aerospace, Transportation, Energy, Manufacturing, Financial Services, Government, Health Care, Telecoms, IT Services, and Gas & Oil in different countries and continents.

Marcos was a Canadian MVP in System Center Cloud & Datacenter Managenment and he has +14 years as Microsoft Certified, with more than 100+ certifications (MCT, MCSE, and MCITP, among others). Marcos is also certified in VMware, CompTIA and ITIL v3. He assisted Microsoft in the development of workshops and special events on Private & Hybrid Cloud, Azure, System Center, Windows Server, Hyper-V and as a speaker at several Microsoft TechEd/Ignite and communities events around the world.

Leave a Reply

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