Questions? Feedback? powered by Olark live chat software
Přeskočit navigaci

Step through creating a Resource Manager-based Azure Virtual Machine with PowerShell

Publikováno dne 11 června, 2015

Senior Content Developer, Azure Documentation

With Azure Service Management, many of the functions associated with creating a virtual machine, such a creating a network interface card and specifying the names of disk files stored in Azure, are automated on your behalf. With Azure Resource Manager, the manual construction of a Resource Manager-based virtual machine must take into account the separate compute, network, and storage components when building a new virtual machine. For example, when you create a Resource Manager-based virtual machine with an Azure Resource Manager template, the template contains the instructions to create and associate all the compute, network, and storage resources together. When you create a Resource Manager-based virtual machine with Azure PowerShell, you must build the set of Resource Manager-based PowerShell commands that request the required resources in the correct order. The new Create and preconfigure a Windows virtual machine with Resource Manager and Azure PowerShell article describes a process to build the PowerShell command set for a Resource Manager-based virtual machine. This process includes the following elements:

  • A resource group (required)
  • A storage account (required)
  • A virtual network (required)
  • An availability set (optional)
  • A network interface card (NIC) and public IP address (both required)
  • A DNS domain name label (optional)
  • Additional data disks (optional)
  • The publisher, offer, and SKU of the Azure virtual machine image (required)
  • The operating system disk name (required)

Here is the resulting PowerShell command set created using the above elements for a virtual machine that:

  • Is in the existing LOBServers resource group
  • Uses the Windows Server 2012 R2 Datacenter image
  • Has the name LOB07 and is in the existing WEB_AS availability set
  • Has a NIC with a public IP address in the FrontEnd subnet (subnet index 0) of the existing AZDatacenter virtual network
  • Has an additional data disk of 200 GB

NOTE: This blog post contains commands for Azure PowerShell 0.9.8 and previous versions. To use these commands with the Azure PowerShell 1.0 Preview, remove the Switch-AzureMode AzureResourceManager command and change all instances of "-Azure" to "-AzureRM". For more information, see Azure PowerShell 1.0 Preview.

 

# Switch to the Resource Manager mode   
Switch-AzureMode AzureResourceManager

# Set values for existing resource group and storage account names
$rgName="LOBServers"
$locName="West US"
$saName="contosoLOBServersSA"

# Set the existing virtual network and subnet index
$vnetName="AZDatacenter"
$subnetIndex=0
$vnet=Get-AzurevirtualNetwork -Name $vnetName -ResourceGroupName $rgName

# Create the NIC
$nicName="AzureInterface"
$domName="contoso-vm-lob07"
$pip=New-AzurePublicIpAddress -Name $nicName -ResourceGroupName $rgName -DomainNameLabel $domName -Location $locName -AllocationMethod Dynamic
$nic=New-AzureNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $pip.Id

# Specify the name, size, and existing availability set
$vmName="LOB07"
$vmSize="Standard_A3"
$avName="WEB_AS"
$avSet=Get-AzureAvailabilitySet –Name $avName –ResourceGroupName $rgName
$vm=New-AzureVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $avset.Id

# Add a 200 GB additional data disk
$diskSize=200
$diskLabel="APPStorage"
$diskName="21050529-DISK02"
$storageAcc=Get-AzureStorageAccount -ResourceGroupName $rgName -Name $saName
$vhdURI=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName  + ".vhd"
Add-AzureVMDataDisk -VM $vm -Name $diskLabel -DiskSizeInGB $diskSize -VhdUri $vhdURI -CreateOption empty

# Specify the image and local administrator account, and then add the NIC
$pubName="MicrosoftWindowsServer"
$offerName="WindowsServer"
$skuName="2012-R2-Datacenter"
$cred=Get-Credential -Message "Type the name and password of the local administrator account." 
$vm=Set-AzureVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureVMNetworkInterface -VM $vm -Id $nic.Id

# Specify the OS disk name and create the VM
$diskName="OSDisk"
$storageAcc=Get-AzureStorageAccount -ResourceGroupName $rgName -Name $saName
$osDiskUri=$storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName  + ".vhd"
$vm=Set-AzureVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureVM -ResourceGroupName $rgName -Location $locName -VM $vm

Once you have created a command set for a VM that you intend to create over and over again, you can save it as a PowerShell script file (*.ps1). Enjoy!