How to provision an Ubuntu machine in Microsoft Azure using PowerShell? (May 31, 2017)


How to provision an Ubuntu machine in Microsoft Azure using PowerShell?

In this post, we saw how to provision an Ubuntu machine using Docker Machine on Azure. In this post, I am going to show you how to provision an Ubuntu VM using PowerShell on Azure.

There are a couple of different ways to provision Azure resources and here in this post, we are going to use imperative option to provision an Ubuntu machine.

First thing we need to install Azure PowerShell Module. It is really simple, yes, running one command and that is it. But you need to have PowerShell version 5.0. You can check the version of your PowerShell by typing $PSVersionTable in your Integrated Scripting Editor ISE.

 

If you are on an older version, you need to get Windows Management Framework 5.0 from here.

We also need to install NuGet version 2.8.5.201 or newer using below command with admin user:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 –Force

 

 

Now that we have PowerShell version 5.0 and NuGet installed, we can install the Azure PowerShell Module using Install-Module command as below:

Install-Module -Name AzureRM -Scope CurrentUser -Force;

 

 

It starts installing a lot of modules e.g. AzureRM.Storage, AzureRM.Network. In order to verify the installation use below command:

 Get-Module -ListAvailable -Name AzureRM

And you will see below:

 

Now our next step is to login to Azure with your current Azure subscription.  Use below command to login to Azure:

Login-AzureRmAccount;

 

 

 

 

 

Now, we need to create a resource group and a storage account using below:

 

$resourceGroup ='armvmLinuxEhsan'

$location = 'Canada Central'

New-AzureRmResourceGroup -Name $resourceGroup -Location $location

$storageAccount = 'ehsanstorage1'

New-AzureRmStorageAccount -Name $storageAccount -ResourceGroupName $resourceGroup -Location $location -Type Standard_GRS

 

 You can also see in Azure portal that it has been created successfully.

 

Next we need to create virtual network for our VM as below:

 

 

And here is the result on Azure Portal:

 

 

Finally use below code to create a Linux VM:

 

 

$nic2Name = 'vm2-ehsan'

$publicIP2 = New-AzureRmPublicIpAddress -Name $nic2Name -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic

$nic2 =New-AzureRmNetworkInterface -Name $nic2Name -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIP2.Id

 

$vm2name = 'ubuntu-ehsan'

$vm2 = New-AzureRmVMConfig -VMName $vm2name -VMSize 'Basic_A1'

 

$cred2 = Get-Credential -Message 'Enter the Credentials'

$vm2 = Set-AzureRmVMOperatingSystem -VM $vm2 -Linux -ComputerName $vm2Name -Credential $cred2

$vm2 = Set-AzureRmVMSourceImage -VM $vm2 -PublisherName 'canonical' -Offer 'UbuntuServer' -Skus '14.04.4-LTS' -Version 'latest'

$vm2 = Add-AzureRmVMNetworkInterface -VM $vm2 -Id $nic2.Id

 

$disk2Name = 'ub-os-disk'

$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount

$osDisk2 = $storageAcc.PrimaryEndpoints.Blob.ToString() + 'vhds/' + $disk2Name + '.vhd'

$vm2 = Set-AzureRmVMOSDisk -VM $vm2 -Name $disk2Name -VhdUri $osDisk2 -CreateOption FromImage

 

New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm2 -Verbose

 

 

 

 

 

In this post, we’ve seen the process of provisioning an Ubuntu VM in Microsoft Azure, using the Azure Resource Manager (ARM) PowerShell module.

 

Back to All Articles

Number of Views:356