1
votes

1) I am trying to create VMs in azure using power-shell. There are multiple ways to create VMs. What is the recommended approach?

2) If the VM is created using Resource Manager, I am not able to find it in the current portal. Only preview portal is differentiating the VMs as "Virtual Machines (Classic)" & "Virtual Machines". When I click on the "Virtual Machines" and the created VM, it is not showing an option to capture VM. How to capture VM (in portal) that is created using Resource Manager powershell?

3) If the subscription has VMs of both types(classic & ARM) how to collect the inventory for both VM types?

3

3 Answers

0
votes

ANS 1. Your approach depends on your requirement. If you need classic VM, go with ASM(Azure Service Manager) approach of spinning VM. If you are going with ARM(Azure Resource Manager) approach follow ARM cmdlets. I would recommend ARM as it is latest and as per MS they will depreciate ASM is future.

ANS 2. New portal doesn't have capture vm option. That option is only for classic vms. Rather it has the same functionality other way around, You can select a vhd and create a vm out of it using json template.

ANS 3. Almost every resource is listed in new portal however new vms can't be seen in old portal. If you are using latest PS cmdlets (1.0.1) even switching is not required. I recommend powershell as the output data is detailed.

0
votes

First - use Preview Portal (portal.azure.com) , the new generation VM are available here only. VMs created using the preview portal are new generation VMs, and Must be created with Resource manager Cmdlets, try to group your VMs of a particular solution in a resource group, and then you can manage and deploy the resource group as a logical unit. so yes, First create a resource group, and then make the VM's which are belonging to this resource, a better approach is to make a template and add all your vm's to that template and deploy from template. follow this https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/

0
votes

The classic VM can create by Azure Service Model (ASM) cmdlets. See the code snippets below.

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"

More details please read this sample post https://gallery.technet.microsoft.com/How-to-create-Azure-VM-by-b894d750