How to include “Plan information” when creating ARM VM from a captured image using Powershell?

岁酱吖の 提交于 2019-11-28 04:26:47

问题


I have a ARM VM created from a Marketplace: bitnami LAMP (Ubuntu) I've successfully captured an image. During the capture I've saved the json template.

Using a template based on that I can successfully create new VMs via the portal's Template Deployment facility interactively. (so the captured image is OK). Please note: That json template do include plan information, see below

However my original goal is to create new ARM VMs based on the captured image using Powershell

All seems to work however in the last command New-AzureRmVM returns and error stating:

Creating a virtual machine from Marketplace image requires Plan information in the request.

Obviously this information is missing, but I can not find out how to add it.

Here is what I've tried:

  • I've examined the $vm variable (what is the parameter of the New-AzureRmVM command) and its Plan property is empty. (as expected)
  • I've searched for appropiate Add-AzureRmVm... commands with no success
  • I've tried to set manually the Plan property and its subproperties in all caseing combinations: all thows error. (like $vm.Plan.Publisher="bitnami")

Actually the original capture's json template contains that Plan intomation:

  },
  "name": "[parameters('vmName')]",
  "type": "Microsoft.Compute/virtualMachines",
  "location": "westeurope",
  "plan": {
    "name": "5-6",
    "publisher": "bitnami",
    "product": "lampstack"
  } 

Again, the captured image (the .vhd) what this script tries to use is confirmed OK, because with the very same captured image I can create new ARM VMs via the portal's Template Deployment facility.


I think the source is not too important this case (there are no error in it, just missing things, but that missing thing is clearly stated in the question) but I attach the source anyway... Optional reading.

# Existing resource parameters
$subscriptionName =  'Visual Studio Premium with MSDN'
$rgName = "rg-wp"
$location = "westeurope"
$stName = 'mystorage' 
$sourceImageUri = 'https://mystorage.blob.core.windows.net/system/Microsoft.Compute/Images/vhds/template-osDisk.be7b0cf4-a28b-47f9-89c7-43887f1570ab.vhd' 

# Creation settings:
$vmSize = 'Standard_DS2'
$vmSuffix = 'wp-11'

#Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionName $subscriptionName

# Get the storage account
#$storageAccount = Get-AzureRmStorageAccount | ? StorageAccountName -EQ $stName
$storageAccount = Get-AzureRmStorageAccount -AccountName $stName -ResourceGroupName $rgName 

# Enable verbose output and stop on error
$VerbosePreference = 'Continue'
#$ErrorActionPreference = 'Stop'

$adminUsername = 'myusername'
$adminPassword = 'mypassword'

$vmName = '{0}-vm' -f $vmSuffix
$nicName = '{0}-nic' -f $vmSuffix
$ipName = '{0}-pip' -f $vmSuffix
$domName = '{0}-mzpx' -f $vmSuffix
$vnetName = '{0}-vn' -f $vmSuffix
$nsgName= '{0}-nsg' -f $vmSuffix


# Networking:
Write-Verbose 'Creating Virtual Network'  
$vnetDef = New-AzureRmVirtualNetwork -ResourceGroupName $rgName -Location $location -Name $vnetName -AddressPrefix '10.0.0.0/16'
Write-Verbose 'Adding subnet to Virtual Network'  
$vnet = $vnetDef | Add-AzureRmVirtualNetworkSubnetConfig -Name 'Subnet-1' -AddressPrefix '10.0.0.0/24' | Set-AzureRmVirtualNetwork

Write-Verbose 'Creating Public IP'  
$pip = New-AzureRmPublicIpAddress -ResourceGroupName $rgName -Location $location -Name $ipName -DomainNameLabel $domName -AllocationMethod Dynamic
Write-Verbose 'Creating NIC'  
$nsg = New-AzureRmNetworkSecurityGroup -Name $nsgName -ResourceGroupName $rgName -Location $location
Write-Verbose 'Network Security Group'  
$nic = New-AzureRmNetworkInterface -ResourceGroupName $rgName -Location $location -Name $nicName -PublicIpAddressId $pip.Id -SubnetId $vnet.Subnets[0].Id -NetworkSecurityGroupId $nsg.Id

# Configuring VM
Write-Verbose 'Creating VM Config'  
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize 

# Specify local administrator account, and then add the NIC
$cred = New-Object PSCredential $adminUsername, ($adminPassword | ConvertTo-SecureString -AsPlainText -Force) # you could use Get-Credential instead to get prompted
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Linux -ComputerName $vmName -Credential $cred 
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

# Specify the OS disk
$diskName = '{0}-osdisk' -f $vmSuffix
$osDiskUri = '{0}vhds/{1}{2}.vhd' -f $storageAccount.PrimaryEndpoints.Blob.ToString(), $vmName.ToLower(), $diskName
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage -SourceImageUri $sourceImageUri -Linux

Write-Verbose 'Creating VM...'  

$result = New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vm

回答1:


As of five days ago, and Azure Powershell version 1.2.2 they added a new cmdlet to the AzureRM.Compute - Set-AzureRmVMPlan

This let's you configure the plan parameter like this -

$vm = New-AzureRmVMConfig -vmName $vmName -vmSize $vmSize

Set-AzureRmVMPlan -VM $vm -Publisher bitnami -Product lampstack -Name "5-6"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $vhdName -VhdUri $vhdUri -Linux -CreateOption attach -Verbose



回答2:


The following command in azure cli provides the information. Example run below.

azure vm image show --location westus --publisher paloaltonetworks --offer vmseries1 --sku bundle1 --version 7\.1\.1 --json

[
  {
    "id": "/Subscriptions/subscription-id/Providers/Microsoft.Compute/Locations/westus/Publishers/paloaltonetworks/ArtifactTypes/VMImage/Offers/vmseries1/Skus/bundle1/Versions/7.1.1",
    "name": "7.1.1",
    "location": "westus",
    "plan": {
      "publisher": "paloaltonetworks",
      "name": "bundle1",
      "product": "vmseries1"
    },
    "osDiskImage": {
      "operatingSystem": "Linux"
    },
    "dataDiskImages": []
  }
]



回答3:


set-azurermvmplan command fixed the error. We should get all the parameters from automation script section of portal.




回答4:


For anyone else that ends up here like I did, if you don't know the publisher or anything.

Save variables for new vm name $VM_NAME, resource group $RESOURCE_GROUP, and location $REGION, then run the following commands and create associated variables:

VM_NAME=<new-vm-name>
RESOURCE_GROUP=<my-resource-group>
REGION=<my-disks-location>

Get $PLAN_NAME, $PLAN_PRODUCT, $PLAN_PUBLISHER **this command can take forever. you can shorten the wait if you can provide '--publisher', '--offer' or '--sku'.*

az vm image list --location $REGION --all -o table | grep <any-keyword>

PLAN_NAME=<output-from-command-above>
PLAN_PRODUCT=<output-from-command-above>
PLAN_PUBLISHER=<output-from-command-above>

Get available disks and save to variables $OS_DISK and $OS_TYPE

az disk list -o table

OS_DISK=<output-from-command-above>
OS_TYPE=<output-from-command-above>

Get list of nics (if available) and save to variables $NIC

az network nic list -o table

NIC=<output-from-command-above>

Get available vm sizes and save to variables $VM_SIZE

az vm list-sizes -l $REGION -o table

VM_SIZE=<output-from-command-above>

Once all the variables are set you can run this command to recreate the machine:

az vm create \
  --name $VM_NAME \
  --resource-group $RESOURCE_GROUP \
  --attach-os-disk $OS_DISK \
  --os-type $OS_TYPE \
  --location $REGION \
  --size $VM_SIZE \
  --plan-name $PLAN_NAME \
  --plan-product $PLAN_PRODUCT \
  --plan-publisher $PLAN_PUBLISHER \
  --nics $NIC


来源:https://stackoverflow.com/questions/35871496/how-to-include-plan-information-when-creating-arm-vm-from-a-captured-image-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!