Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

azure resource manager - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...