Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 284
g.pickardou Avatar asked Mar 08 '16 15:03

g.pickardou


People also ask

Can I create a new arm VM from a captured image?

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.

How do I create a VM image using PowerShell?

To create the VM image using PowerShell, follow these next steps: First, define some variables that are relevant to the virtual machine. These variables will store the values of the resource group, virtual machine name, location and the intended name for the image to be created. See the example below.

How to capture a specific VM in azure?

With classic azure, you can capture specialized VMs as use the resulting image as a quick backup. With ARM azure, the capture command expects the VM to be sysprep'ed/generalized and will actually make the original VM not boot normally anymore.

How do I create multiple virtual machines (VMs) from an azure image?

You can create multiple virtual machines (VMs) from an Azure managed VM image using the Azure portal or PowerShell. A managed VM image contains the information necessary to create a VM, including the OS and data disks. The virtual hard disks (VHDs) that make up the image, including both the OS disks and any data disks, are stored as managed disks.


2 Answers

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
like image 94
Michael B Avatar answered Oct 05 '22 10:10

Michael B


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": []
  }
]
like image 44
techcrunchone Avatar answered Oct 05 '22 12:10

techcrunchone