使用专用化映像创建 VMCreate a VM using a specialized image

从共享映像库中存储的专用化映像版本创建 VM。Create a VM from a specialized image version stored in a Shared Image Gallery. 要使用通用化映像版本创建 VM,请参阅使用通用化映像创建 VMIf want to create a VM using a generalized image version, see Create a VM using a generalized image.

获得专用化映像版本后,可以创建一个或多个新 VM。Once you have a specialized image version, you can create one or more new VMs. 使用 New-AzVM cmdlet。Using the New-AzVM cmdlet.

在此示例中,我们使用映像定义 ID 来确保新 VM 会使用最新版本的映像。In this example, we are using the image definition ID to ensure your new VM will use the most recent version of an image. 也可通过将映像版本 ID 用作 Set-AzVMSourceImage -Id 来使用特定版本。You can also use a specific version by using the image version ID for Set-AzVMSourceImage -Id. 例如,若要使用映像版本 1.0.0,请键入:Set-AzVMSourceImage -Id "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"For example, to use image version 1.0.0 type: Set-AzVMSourceImage -Id "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0".

请注意,使用特定映像版本意味着:如果该特定映像版本由于已删除或已从区域中删除而无法使用,则自动化可能会失败。Be aware that using a specific image version means automation could fail if that specific image version isn't available because it was deleted or removed from the region. 建议使用映像定义 ID 来创建新的 VM(除非需要特定的映像版本)。We recommend using the image definition ID for creating your new VM, unless a specific image version is required.

在此示例中,请根据需要替换资源名称。Replace resource names as needed in this example.


# Create some variables for the new VM.

$resourceGroup = "mySIGSpecializedRG"
$location = "China East"
$vmName = "mySpecializedVM"

# Get the image. Replace the name of your resource group, gallery, and image definition. This will create the VM from the latest image version available.

$imageDefinition = Get-AzGalleryImageDefinition `
   -GalleryName myGallery `
   -ResourceGroupName myResourceGroup `
   -Name myImageDefinition

# Create a resource group
New-AzResourceGroup -Name $resourceGroup -Location $location

# Create the network resources.

$subnetConfig = New-AzVirtualNetworkSubnetConfig `
   -Name mySubnet `
   -AddressPrefix 192.168.1.0/24
$vnet = New-AzVirtualNetwork `
   -ResourceGroupName $resourceGroup `
   -Location $location `
   -Name MYvNET `
   -AddressPrefix 192.168.0.0/16 `
   -Subnet $subnetConfig
$pip = New-AzPublicIpAddress `
   -ResourceGroupName $resourceGroup `
   -Location $location `
  -Name "mypublicdns$(Get-Random)" `
  -AllocationMethod Static `
  -IdleTimeoutInMinutes 4
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig `
   -Name myNetworkSecurityGroupRuleRDP  `
   -Protocol Tcp `
   -Direction Inbound `
   -Priority 1000 `
   -SourceAddressPrefix * `
   -SourcePortRange * `
   -DestinationAddressPrefix * `
   -DestinationPortRange 3389 -Access Allow
$nsg = New-AzNetworkSecurityGroup `
   -ResourceGroupName $resourceGroup `
   -Location $location `
   -Name myNetworkSecurityGroup `
   -SecurityRules $nsgRuleRDP
$nic = New-AzNetworkInterface `
   -Name $vmName `
   -ResourceGroupName $resourceGroup `
   -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration using Set-AzVMSourceImage -Id $imageDefinition.Id to use the latest available image version.

$vmConfig = New-AzVMConfig `
   -VMName $vmName `
   -VMSize Standard_D1_v2 | `
   Set-AzVMSourceImage -Id $imageDefinition.Id | `
   Add-AzVMNetworkInterface -Id $nic.Id

# Create a virtual machine
New-AzVM `
   -ResourceGroupName $resourceGroup `
   -Location $location `
   -VM $vmConfig

附加数据磁盘Attach the data disk

如果映像包含数据磁盘,则需要将数据磁盘连接到 VM。If your image contained a data disk, you need to attach the data disk to the VM.


$vm = Get-AzVM -Name $vmName -ResourceGroupName $resourceGroup 

$lun = $imageVersion.StorageProfile.DataDiskImages.Lun

Add-AzVMDataDisk `
   -CreateOption FromImage `
   -SourceImageUri $imageversion.Id `
   -Lun $lun `
   -Caching $imageVersion.StorageProfile.DataDiskImages.HostCaching `
   -DiskSizeInGB $imageVersion.StorageProfile.DataDiskImages.SizeInGB `
   -VM $vm

后续步骤Next steps

此外可以使用模板创建共享映像库资源。You can also create Shared Image Gallery resource using templates. 提供多个 Azure 快速入门模板:There are several Azure Quickstart Templates available:

有关共享映像库的详细信息,请参阅概述For more information about Shared Image Galleries, see the Overview. 如果遇到问题,请参阅排查共享映像库问题If you run into issues, see Troubleshooting shared image galleries.