How to create Azure VM by Powershell using classic ASM API


This sample demonstrates how to make use of classic Azure ASM (Azure Service Model) API to create classic VM on Azure platform.


Related Topics


Currently, there exists two kinds of API to manage Azure Resource. One is the latest API suite ARM (Azure Resource Management” API, the other is the old classic ASM (Azure Service Model) API. Both API suites can be used to create VM. However, only the classic ASM API can create classic VM that can be viewed on the classic Azure Portal .


IT Pros have the requirement to automate the VM creation job through Powershell. They cannot find enough material which can instruct them how to go through the classic ASM API to create VM. This sample tries to amend the instruction.


Install Azure Powershell according to


• Open the script file ASMCreateVM.ps1

• Edit the variable $StorageLocation and $VMName and then save the file


# Set the location of the storage account, service and VM image 
$StorageLocation = "East Asia" # See the ValidateSet attribute of parameter $Location in the function below 
# Set the name of vm 
$VMName = "vmdemon20160812" # !!!! the name of vm should be lower case 


• Open the powershell and run the script file ASMCreateVM.ps1

• You will need to input the VM admin username and password in progress. Be aware, the password should be strong enough to obey the rule “At least 9 characters. Specifically, they should have uppercase, lowercase character and symbol”

• After the script has finished the job, you will see the similar output at following



Here are some code snippets for your references.

# Set the location of the storage account, service and VM image 
$StorageLocation = "East Asia" # See the ValidateSet attribute of parameter $Location in the function below 
# Set the name of vm 
$VMName = "vmdemo20160812" # !!!! the name of vm should be lower case 
Function New-VMByASM 
        [Parameter(Mandatory=$true)][String] $VMName, 
        [Parameter(Mandatory=$false)][String] $VMLabelPattern = "*Windows Server 2012 Datacenter*", 
        [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", 
        [String]$VMSize = "Basic_A0" 
    # 1. Login Azure by admin account 
    # 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"


Additional Resources

• Installing Azure Powershell:

• Azure ASM create VM resource:

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.