使用 powershell 创建 azure VM 的推荐方法是什么?

What is the recommended approach to create azure VM using powershell?

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

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

创建的 VM(在门户中)

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

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

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

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

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

经典 VM 可以通过 Azure 服务模型 (ASM) cmdlet 创建。请参阅下面的代码片段。

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"

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