5

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
4

5 回答 5

7

截至五天前,他们在 Azure Powershell 1.2.2 版中添加了一个新的 cmdlet 到 AzureRM.Compute -Set-AzureRmVMPlan

这让您可以像这样配置计划参数 -

$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
于 2016-03-08T17:19:57.350 回答
1

对于像我一样在这里结束的其他人,如果你不知道出版商或任何东西。

保存新虚拟机名称 $VM_NAME、资源组 $RESOURCE_GROUP 和位置 $REGION 的变量,然后运行以下命令并创建关联变量:

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

Get $PLAN_NAME, $PLAN_PRODUCT, $PLAN_PUBLISHER **这个命令可能需要很长时间。如果您可以提供“--publisher”、“--offer”或“--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>

获取可用磁盘并保存到变量 $OS_DISK 和 $OS_TYPE

az disk list -o table

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

获取 nics 列表(如果可用)并保存到变量 $NIC

az network nic list -o table

NIC=<output-from-command-above>

获取可用的 vm 大小并保存到变量 $VM_SIZE

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

VM_SIZE=<output-from-command-above>

设置完所有变量后,您可以运行此命令来重新创建机器:

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
于 2019-06-12T07:57:42.043 回答
1

azure cli 中的以下命令提供了信息。下面运行示例。

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": []
  }
]
于 2016-12-19T19:25:23.233 回答
0

如果您只想“编辑”/更新虚拟机并将计划轻松添加到其中,但收到错误消息:

Azure 错误:CannotSetPlanOnUpdate

消息:此资源是在没有计划的情况下创建的。新计划不能与更新相关联。

您可以使用以下 VM 配置检索 PowerShell 对象:

$vm = Get-AzureRmVM -ResourceGroupName "<Res-Grp>" -Name "<VM-Name>"

然后按照 michael-b 的建议进行编辑

Set-AzureRmVMPlan -VM $vm -Publisher bitnami -Product lampstack -Name "5-6"

然后通过 Portal 删除 VM(不要关闭 Powershell 窗口!)然后重新部署:

Update-AzureRmVM -VM $vm
于 2020-04-09T17:17:40.877 回答
0

set-azurermvmplan 命令修复了该错误。我们应该从门户的自动化脚本部分获取所有参数。

于 2017-05-05T15:54:19.540 回答