【问题标题】:What is the recommended approach to create azure VM using powershell?使用 powershell 创建 azure VM 的推荐方法是什么?
【发布时间】:2016-08-15 14:47:48
【问题描述】:

1) 我正在尝试使用 power-shell 在 azure 中创建 VM。有多种方法可以创建 VM。推荐的方法是什么?

2) 如果虚拟机是使用资源管理器创建的,我无法在当前门户中找到它。只有预览门户将 VM 区分为“虚拟机(经典)”和“虚拟机”。当我单击“虚拟机”和创建的 VM 时,它没有显示捕获 VM 的选项。如何捕获使用 Resource Manager powershell 创建的 VM(在门户中)?

3) 如果订阅有两种类型(经典和 ARM)的虚拟机,如何收集两种虚拟机类型的清单?

【问题讨论】:

    标签: azure azure-powershell azureportal


    【解决方案1】:

    ANS 1. 您的方法取决于您的要求。如果您需要经典 VM,请使用旋转 VM 的 ASM(Azure 服务管理器)方法。如果您使用 ARM(Azure 资源管理器)方法,请遵循 ARM cmdlet。我会推荐 ARM,因为它是最新的,并且根据 MS,他们会贬低 ASM 的未来。

    ANS 2. 新门户没有捕获 vm 选项。该选项仅适用于经典虚拟机。相反,它具有相同的功能,您可以选择一个 vhd 并使用 json 模板从中创建一个 vm。

    ANS 3. 几乎所有资源都列在新门户中,但是在旧门户中看不到新虚拟机。如果您使用的是最新的 PS cmdlet (1.0.1),则甚至不需要切换。我推荐powershell,因为输出数据很详细。

    【讨论】:

      【解决方案2】:

      首次使用预览门户 (portal.azure.com) ,新一代 VM 仅在此处可用。 使用预览门户创建的 VM 是新一代 VM,必须使用资源管理器 Cmdlet 创建,尝试将特定解决方案的 VM 分组到资源组中,然后您可以将资源组作为逻辑单元进行管理和部署。 所以是的,首先创建一个资源组,然后制作属于该资源的虚拟机,更好的方法是制作一个模板并将所有虚拟机添加到该模板并从模板部署。 关注这个https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/

      【讨论】:

      • 嗨@Brij Raj Singh。感谢您的快速回复。还有一个问题……如果订阅已经有经典虚拟机怎么办?我们是否必须切换模式来管理这些虚拟机?创建经典 VM 时会在内部创建 RG,但使用 Powershell ARM 时未显示它们,对吗?
      • 到目前为止,claasic 和新 VM 可以同时存在于同一个订阅中。只是追踪的方式不同。如果您正在寻找经典 vm,请遵循 ASM cmdlet,如果您正在寻找 ARM vm,请遵循 ARM cmdlet。微软未来将贬值云服务方式,可能云服务将转换为资源组。根据 MS 的最新更新,一切都将在 ARM 上运行。
      • @Atf 说的很对,如果订阅已经有经典 VM,他们将继续保留,但旧门户肯定会消失,创建 VM 的唯一选择将是新一代 VM portal.azure.com 的新门户,是的,即使您不制作 RG,也会创建一个,所以我建议您最好制作一个作为一种好习惯,否则有一天您将拥有大量 RG,并且很难知道是哪一个是哪个,它还可以帮助您保持仪表板整洁。
      【解决方案3】:

      经典 VM 可以通过 Azure 服务模型 (ASM) cmdlet 创建。参见下面的代码 sn-ps。

      Function New-VMByASM 
      { 
          [CmdletBinding()] 
          Param 
          ( 
      
              [Parameter(Mandatory=$true)][String] $VMName, 
              [Parameter(Mandatory=$false)][String] $VMLabelPattern = "*Windows Server 2012 Datacenter*", 
      
              [Parameter(Mandatory=$false)] 
              [ValidateSet("North Europe", "East US", "South Central US", "Central US", "East US 2", "West US", "West Europe", "Southeast Asia", "East Asia", "Japan West", "Japan East")] 
              [String]$Location = "East Asia", 
      
              [Parameter(Mandatory=$false)] 
              [ValidateSet("ExtraSmall", "Small", "Medium", "Large", "ExtraLarge", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "Basic_A0", "Basic_A1", "Basic_A2", "Basic_A3", "Basic_A4", "Standard_D1", "Standard_D2", "Standard_D3", "Standard_D4", "Standard_D11", "Standard_D12", "Standard_D13", "Standard_D14", "Standard_D1_v2", "Standard_D2_v2", "Standard_D3_v2", "Standard_D4_v2", "Standard_D5_v2", "Standard_D11_v2", "Standard_D12_v2", "Standard_D13_v2", "Standard_D14_v2", "Standard_DS1", "Standard_DS2", "Standard_DS3", "Standard_DS4", "Standard_DS11", "Standard_DS12", "Standard_DS13", "Standard_DS14", "Standard_DS1_v2", "Standard_DS2_v2", "Standard_DS3_v2", "Standard_DS4_v2", "Standard_DS5_v2", "Standard_DS11_v2", "Standard_DS12_v2", "Standard_DS13_v2", "Standard_DS14_v2", "Standard_G1", "Standard_G2", "Standard_G3", "Standard_G4", "Standard_G5", "Standard_GS1", "Standard_GS2", "Standard_GS3", "Standard_GS4", "Standard_GS5", "Standard_F1", "Standard_F2", "Standard_F4", "Standard_F8", "Standard_F16", "Standard_F1s", "Standard_F2s", "Standard_F4s", "Standard_F8s", "Standard_F16s")] 
              [String]$VMSize = "Basic_A0" 
          ) 
          # 1. Login Azure by admin account 
          Add-AzureAccount 
          # 
          # 2. Select subscription name 
          $subscriptionName = Get-AzureSubscription | Select -ExpandProperty SubscriptionName 
          # 
          # 3. Create storage account 
          $storageAccountName = $VMName  
          # here we use VMName to play the storage account name and create it, you can choose your name or use existed one to replace the storage account creation operation 
          New-AzureStorageAccount -StorageAccountName $storageAccountName -Location $Location | Out-Null 
          # 
          # 4. Select subscription name and storage account name for current context 
          Select-AzureSubscription -SubscriptionName $subscriptionName -Current | Out-Null 
          Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccountName $storageAccountName | Out-Null 
          # 
          # 5. Select a VM image name 
          $label = $VMLabelPattern 
          # take care, please ensure the VM image location resides to the same location of your storage account and service below 
          $imageName = Get-AzureVMImage | where { $_.Label -like $label } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1 
          # 
          # 6. Create cloud service 
          $svcName = $VMName 
          # here we use VMName to play the service name and create it, you can choose your name or use existed one to replace the service creation operation 
          New-AzureService -ServiceName $svcName -Location $Location | Out-Null 
          # 
          # 7. Build command set 
          $vmConfig = New-AzureVMConfig -Name $VMName -InstanceSize $VMSize -ImageName $imageName 
          # 
          # 8. Set local admin of this vm 
          $cred=Get-Credential -Message "Type the name and password of the local administrator account." 
          $vmConfig | Add-AzureProvisioningConfig -Windows -AdminUsername $cred.Username -Password $cred.GetNetworkCredential().Password 
          # 
          # 9. Execute the final cmdlet to create the VM 
          New-AzureVM -ServiceName $svcName -VMs $vmConfig | Out-Null 
      } 
      
      New-VMByASM -VMName $VMName -Location $StorageLocation 
      Write-Host "Done"
      

      更多详情请阅读此示例帖子https://gallery.technet.microsoft.com/How-to-create-Azure-VM-by-b894d750

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多