使用通用化映像创建 VMCreate a VM using a generalized image

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

获得通用化映像版本后,可以创建一个或多个新 VM。Once you have a generalized 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 these examples.

简化参数集Simplified parameter set

可以使用简化的参数集从映像快速创建 VM。You can use the simplified parameter set to quickly create a VM from an image. 简化的参数集使用 VM 名称自动为你创建一些必需的资源,例如 vNet 和公共 IP 地址。The simplified parameter set uses the VM name to automatically create some of the required resources, like vNet and public IP address, for you.

# Create some variables for the new VM 
$resourceGroup = "myResourceGroup"
$location = "China East"
$vmName = "myVMfromImage"

# 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 user object
$cred = Get-Credential `
   -Message "Enter a username and password for the virtual machine."

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

New-AzVM `
   -ResourceGroupName $resourceGroup `
   -Location $location `
   -Name $vmName `
   -Image $imageDefinition.Id

完整参数集Full parameter set

可以借助完整参数集使用特定资源创建 VM。You can create a VM using specific resources by using the full parameter set.

# Create some variables for the new VM 
$resourceGroup = "myResourceGroup"
$location = "China East"
$vmName = "myVMfromImage"

# 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 user object
$cred = Get-Credential `
   -Message "Enter a username and password for the virtual machine."

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

# Network pieces
$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 myNic `
   -ResourceGroupName $resourceGroup `
   -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

# Create a virtual machine configuration using $imageDefinition.Id to use the latest image version.
$vmConfig = New-AzVMConfig `
   -VMName $vmName `
   -VMSize Standard_D1_v2 | `
   Set-AzVMOperatingSystem -Windows -ComputerName $vmName -Credential $cred | `
   Set-AzVMSourceImage -Id $imageDefinition.Id | `
   Add-AzVMNetworkInterface -Id $nic.Id

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

后续步骤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.