azure-powershell - 使用 Powershell 从捕获的图像创建 ARM VM 时如何包含 "Plan information"?

标签 azure-powershell azure-resource-manager azureportal

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

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

然而,我最初的目标是基于捕获的图像创建新的 ARM VM 使用 Powershell

然而,在 New-AzureRmVM 返回的最后一个命令中,一切似乎都有效,错误说明:

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



显然缺少此信息,但我不知道如何添加它。

这是我尝试过的:
  • 我检查了 $vm 变量(New-AzureRmVM 命令的参数是什么),它的 Plan 属性为空。 (如预期)
  • 我搜索了合适的 Add-AzureRmVm... 命令但没有成功
  • 我尝试在 中手动设置 Plan 属性及其子属性。所有 shell 组合:所有错误。 (如 $vm.Plan.Publisher="bitnami")

  • 实际上,原始捕获的 json 模板包含该计划内容:
      },
      "name": "[parameters('vmName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "location": "westeurope",
      "plan": {
        "name": "5-6",
        "publisher": "bitnami",
        "product": "lampstack"
      } 
    

    同样,此脚本尝试使用的捕获镜像(.vhd)已确认无误,因为使用完全相同的捕获镜像,我可以通过门户的模板部署工具创建新的 ARM VM。

    我认为在这种情况下来源并不太重要(其中没有错误,只是缺少一些东西,但是问题中明确说明了缺少的东西)
    但我还是附上了来源... 选读 .
    # 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
    这让您可以像这样配置计划参数 -

    $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
    

    关于azure-powershell - 使用 Powershell 从捕获的图像创建 ARM VM 时如何包含 "Plan information"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871496/

    相关文章:

    azure - Powershell Azure SQL DB(Yaml 管道)

    azure - 如何使用 PowerShell 安装和配置 Microsoft Monitoring Agent 以与 OMS 网关(代理)一起使用

    .net - 无法通过arm模板部署azure功能应用程序

    azure - 将来自不同资源的 Azure Monitor 图表合并到一个图表中

    azure - 如何从 Automation 帐户登录到 Azure 中的 Application Insights?

    azure - 使用 powershell 获取 Cosmosdb 容器集合项目

    azure - 更新作为参数传递给二头肌文件的对象

    通过资源管理器(或代码)进行 Azure Active Directory B2C 部署

    Azure 搜索无效的身份验证 token

    visual-studio - 使用 azure appcenter 的移动应用程序的诊断日志