Create Azure VM Using Powershell

hen we want to automate the deployment of VM in azure we can use PowerShell as an option

There are two ways of creating a VM using PowerShell

  • Using New-AzureQuickVM
  • Using  New-AzureVMConfig

Using  New-AzureQuickVM 

This Cmdlet allows to provision a single virtual machine with a simple configuration based on an image only. This Cmdlet doesn’t support instantiating VM from a disk.Also can’t specify any additional endpoints or create additional data disk during the creation.
Following is an example that I used to create a VM

$username = "localadmin&amp"
$pasword = 'Pa$$w0rd'
$servicename = "kasunVM"
$location = "Southeast Asia"
$size = "Small"
$vmname = "VM2"
$imagefamily = "Windows Server 2012 R2 Datacenter"
$imagename = Get-AzureVMImage | where {$_.ImageFamily -eq $imagefamily} | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1
New-AzureQuickVM -Windows -ServiceName $servicename -Name $vmname -ImageName $imagename -Password $pasword -AdminUsername $username -Location $location -InstanceSize $size -Verbose

This slideshow requires JavaScript.

Using  New-AzureVMConfig

This cmdlet use the  build up a configuration object for the virtual machine. With this you can add additional options to the VM at creation time such as additional data disk, endpoints, AD domain join information,disable windows updates, specify time zone etc.

First cmdlet to call is New-AzureVMConfig, it creates a local configuration object that can be passed to other Azure cmdlets that support the VM parameter either directly, or using the Windows PowerShell pipeline operator.

In my example I added 10 GB additional data disk and endpoint for SQL (Port 1433)

$username = localadmin"
$pasword = 'Pa$$w0rd'
$servicename = "kasunVM"
$location = "Southeast Asia"
$size = "Small"
$vmname = "VM1"

$imagefamily = "Windows Server 2012 R2 Datacenter"
$imagename = Get-AzureVMImage | where {$_.ImageFamily -eq $imagefamily} | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1


New-AzureVMConfig -Name $vmname -InstanceSize $size -ImageName $imagename | Add-AzureProvisioningConfig -Windows -AdminUsername $username -Password $pasword |
Add-AzureDataDisk -CreateNew -DiskLabel "Data" -DiskSizeInGB "10" -LUN "0" | Add-AzureEndpoint -Name 'Sql' -Protocol tcp -LocalPort 1433 -PublicPort 1433 |
New-AzureVM -ServiceName $servicename -Location $location -Verbose

This slideshow requires JavaScript.

NOTE – When you create additional data disk it wasn’t visible until it initialize through the disk management in the VM

That’s all for this post hope to see all soon 🙂

 

, , ,

Post navigation