如何包括“计划信息"?使用Powershell从捕获的映像创建ARM VM时? [英] How to include "Plan information" when creating ARM VM from a captured image using Powershell?

查看:69
本文介绍了如何包括“计划信息"?使用Powershell从捕获的映像创建ARM VM时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过市场创建的ARM VM:bitnami LAMP(Ubuntu) 我已经成功捕获了图像.在捕获过程中,我已经保存了json模板.

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.

使用基于此的模板,我可以通过门户的模板部署工具以交互方式成功创建新的VM. (因此捕获的图像是可以的).请注意:json模板包含计划信息,请参见下文

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

但是,我最初的目标是使用Powershell

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

在上一个命令New-AzureRmVM返回并显示错误消息时,所有操作似乎都可以正常工作

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

从Marketplace映像创建虚拟机需要计划 请求中的信息.

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

很明显,此信息丢失了,但是我找不到如何添加它.

这是我尝试过的方法:

  • 我已经检查了$ vm变量(New-AzureRmVM命令的参数),并且其Plan属性为空. (如预期)
  • 我一直在搜索不正确的Add-AzureRmVm ...命令
  • 我尝试以 所有大小写 组合手动设置Plan属性及其子属性:所有错误. (例如$ vm.Plan.Publisher ="bitnami")

实际上,原始捕获的json模板包含该计划信息:

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"
  } 

同样,确认此脚本尝试使用的捕获映像(.vhd)也可以,因为使用相同的捕获映像,我可以通过门户网站的模板部署工具创建新的ARM VM.

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

推荐答案

从五天前开始,在Azure Powershell 1.2.2版中,他们向AzureRM.Compute添加了一个新cmdlet.计算-Set-AzureRmVMPlan

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

这使您可以像这样配置plan参数-

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

这篇关于如何包括“计划信息"?使用Powershell从捕获的映像创建ARM VM时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆