在创建映像时提供 Azure 市场购买计划信息Supply Azure Marketplace purchase plan information when creating images

如果使用最初从 Azure 市场映像创建的源在共享库中创建映像,可能需要跟踪购买计划信息。If you are creating an image in a shared gallery, using a source that was originally created from an Azure Marketplace image, you may need to keep track of purchase plan information. 本文介绍了如何查找 VM 的购买计划信息以及之后如何在创建映像定义时使用该信息。This article shows how to find purchase plan information for a VM, then use that information when creating an image definition. 另外还介绍了如何在为映像创建 VM 时使用映像定义中的信息来简化提供购买计划信息。We also cover using the information from the image definition to simplify supplying the purchase plan information when creating a VM for an image.

若要详细了解如何查找和使用市场映像,请参阅查找和使用 Azure 市场映像For more information about finding and using Marketplace images, see Find and use Azure Marketplace images.

获取源 VM 信息Get the source VM information

如果仍有原始 VM,可使用 Get-AzVM 从该 VM 中获取计划、发布者和 sku 信息。If you still have the original VM, you can get the plan, publisher, and sku information from it using Get-AzVM. 此示例获取 myResourceGroup 资源组中名为 myVM 的 VM,然后显示购买计划信息 。This example gets a VM named myVM in the myResourceGroup resource group and then displays the purchase plan information.

$vm = Get-azvm `
   -ResourceGroupName myResourceGroup `
   -Name myVM
$vm.Plan.Publisher
$vm.Plan.Name
$vm.Plan.Product

创建映像定义Create the image definition

获取要用来存储映像的映像库。Get the image gallery that you want to use to store the image. 可以先列出所有的库。You can list all of the galleries first.

Get-AzResource -ResourceType Microsoft.Compute/galleries | Format-Table

然后为需要使用的库创建变量。Then create variables for the gallery you want to use. 在此示例中,我们要在 myGalleryRG 资源组中为 myGallery 创建一个名为 $gallery 的变量 。In this example, we are creating a variable named $gallery for myGallery in the myGalleryRG resource group.

$gallery = Get-AzGallery `
   -Name myGallery `
   -ResourceGroupName myGalleryRG

使用 -PurchasePlanPublisher-PurchasePlanProduct-PurchasePlanName 参数创建映像定义。Create the image definition, using the -PurchasePlanPublisher, -PurchasePlanProduct, and -PurchasePlanName parameters.

 $imageDefinition = New-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -ResourceGroupName $gallery.ResourceGroupName `
   -Location $gallery.Location `
   -Name 'myImageDefinition' `
   -OsState specialized `
   -OsType Linux `
   -Publisher 'myPublisher' `
   -Offer 'myOffer' `
   -Sku 'mySKU' `
   -PurchasePlanPublisher $vm.Plan.Publisher `
   -PurchasePlanProduct $vm.Plan.Product `
   -PurchasePlanName  $vm.Plan.Name

然后使用 New-AzGalleryImageVersion 创建映像版本。Then create your image version using New-AzGalleryImageVersion. 可以从 VM托管映像VHD\snapshot另一映像版本创建映像版本。You can create an image version from a VM, managed image, VHD\snapshot, or another image version.

创建 VMCreate the VM

在从映像创建 VM 时,可以使用映像定义中的信息,以利用 Set-AzVMPlan 传入发布者信息。When you go to create a VM from the image, you can use the information from the image definition to pass in the publisher information using Set-AzVMPlan.

# Create some variables for the new VM.
$resourceGroup = "mySIGPubVM"
$location = "China North"
$vmName = "mySIGPubVM"

# 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. Set-AZVMPlan is used to pass the plan information in for the VM.

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

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

后续步骤Next steps

若要详细了解如何查找和使用市场映像,请参阅查找和使用 Azure 市场映像For more information about finding and using Marketplace images, see Find and use Azure Marketplace images.