Share via


Azure: How to create a Virtual Machine using PowerShell (Part 2)

In previous article we created the virtual machine using Quick VM command.

https://social.technet.microsoft.com/wiki/contents/articles/40000.how-to-create-a-virtual-machine-in-microsoft-azure-using-powershell-part-1.aspx

In this article, we will create the virtual machine with more options and more custom configuration available.

Here, we will define the VM configuration using New-AzureVMConfig command, add the Azure endpoint for port 80 using command Add-AzureEndPoint. You can put more options to these commands as per your choice.

To create the VM we will use command New-AzureVM and pass the configuration values to it.

Here is the below script to create the VM -

#define all the values to variables needed to create virtual machine
 
$location = "South India"
 
$cloudservicename = "kkazurevm02"
 
$vmsize = "Basic_A2"
 
$vmname = "kkazurevm02"
 
$imagefamily = "Windows Server 2012 Datacenter"
 
$imagename = Get-AzureVMImage | where { $_. ImageFamily -eq $imagefamily } | sort PublishedDate -Descending | Select -ExpandProperty ImageName -First 1
 
$username = "kkazure"
 
$password = ""
 
$storagename = "kkazurestore02"
 
#create new storage account
 
New-AzureStorageAccount -StorageAccountName $storagename -Location $location
 
#get the subscription name from login account
 
$subscriptionname = (Get-AzureSubscription).SubscriptionName
 
Set-AzureSubscription -SubscriptionName $subscriptionname -CurrentStorageAccountName $storagename
 
#Assigning configuration, adding Endpooint
 
$config = New-AzureVMConfig -Name $vmName -InstanceSize $vmsize -ImageName $imageName -Label "Win 2012 Server" | Add-AzureProvisioningConfig -Windows -AdminUsername $username -Password $password | Add-AzureEndpoint -Name "HTTP" -Protocol tcp -PublicPort 80 -LocalPort 80 #|
 
#Create new Azure VM
 
New-AzureVM -ServiceName $cloudServiceName -Location $location -VMs $config

Once the script gets executed we will see the following details in the PowerShell window.

We can also check the details of VM using management portal -

In the endpoints section we can see the endpoint HTTP with port number 80 that we specified at the time of creation of virtual machine.

So friends, we can create the virtual machine using PowerShell in this way.

See Also