Q. How can I create an IaaS VM using Azure Resource Manager?

Create Azure IaaS VMs using Azure Resource Manager via PowerShell

John Savill

June 2, 2015

2 Min Read
Q. How can I create an IaaS VM using Azure Resource Manager?
Getty Images

Q. How can I create an IaaS VM using Azure Resource Manager?

A. To create a VM using Azure Resource Manager (ARM) and regular PowerShell, use the following. The resources used need to reside under ARM which means the Virtual Network and Storage Account which is why I create new ones as part of this PowerShell.

Firstly connect to Azure using Azure AD authentication:

Add-AzureAccount

Switch to ARM mode:

Switch-AzureMode -Name AzureResourceManager

Set the subscription to use:

Set-AzureSubscription -SubscriptionName 'Windows Azure Internal Consumption' -CurrentStorageAccount 'savtechstoreeastus' 

For detailed help run:

get-help new-azurevm -examples

which has complete code to create a new Azure VM.

Below are the main points and have some corrections over the example in the help.

 

#Set basic values$rgname = 'SavillTechRGEastUS'$loc = 'East US'$vmsize = 'Standard_A2';$vmname = 'testvm44';# Setup Storage$stoname = 'savtechrgeastuslrs';$stotype = 'Standard_LRS';#Create a new ARM storage accountNew-AzureStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype$stoaccount = Get-AzureStorageAccount -ResourceGroupName $rgname -Name $stoname;# Create VM Object$vm = New-AzureVMConfig -VMName $vmname -VMSize $vmsize;# Setup Networking with a new virtual network with a single subnet$subnet = New-AzureVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24"$vnet = New-AzureVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -DnsServer "10.1.1.1" -Subnet $subnet$vnet = Get-AzureVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname$subnetId = $vnet.Subnets[0].Id#Create a VM NIC attached to the subnet$nic = New-AzureNetworkInterface -Force -Name ('nic' + $vmname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId;$nic = Get-AzureNetworkInterface -Name ('nic' + $vmname) -ResourceGroupName $rgname;$nicId = $nic.Id;# Add NIC to VM$vm = Add-AzureVMNetworkInterface -VM $vm -Id $nicId;# Set OS disk values$osDiskName = 'osDisk';$osDiskCaching = 'ReadWrite';$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";# Setup OS & Image$user = "localadmin";$password = 'Pa55word5';$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);$computerName = $vmname;$vhdContainer = "https://$stoname.blob.core.windows.net/$computerName";Get-AzureVMImage -Location 'East US' -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' Get-AzureVMImageDetail -Location 'East US' -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version '4.0.201504'#Apply the various configurations$vm = Set-AzureVMOperatingSystem -VM $vm -Windows -ComputerName $computerName -Credential $cred;$vm = Set-AzureVMSourceImage -VM $vm -PublisherName 'MicrosoftWindowsServer' -Offer 'WindowsServer' -Skus '2012-R2-Datacenter' -Version '4.0.201504'$vm = Set-AzureVMOSDisk -VM $vm -VhdUri "$vhdContainer/testos.vhd" -name 'TestOS' -CreateOption fromImage# Create Virtual MachineNew-AzureVM -ResourceGroupName $rgname -Location $loc -Name $vmname -VM $vm; 

Below is a successful creation and it will show in the new Azure portal under Virtual machines (v2).

PS C:> New-AzureVM -ResourceGroupName $rgname -Location $loc -Name $vmname -VM $vm; EndTime : 5/26/2015 10:38:15 AM -05:00Error : Output : StartTime : 5/26/2015 10:34:42 AM -05:00Status : SucceededTrackingOperationId : 5517dc8b-947f-4316-b3da-130c8bbb3ed3RequestId : 7f9eee61-27d4-4d6b-ac58-6794a1603f96StatusCode : OK

 

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like