Provisioning Linux Virtual Machines Through PowerShell Commands

In one of my blogs, we have seen about provisioning virtual machines through the Command Line Interface(CLI). Here, in this article, we are going to see how the PowerShell commands are used to create Linux virtual machines. To begin, we shall have the environment configured on our local machine. 
 
To start that, open PowerShell command prompt and install the below Azure module.
  1. Install-Module AzureRM.Netcore  
After installing the module in PowerShell session, we should authenticate the PowerShell session to one of the Azure subscription accounts to create the VM. Run the below code in PowerShell to connect with the Azure subscription.
  1. Connect-AzureRMAccount -Subscription 'Demo Account'  
While executing the above command, there will be a prompt shown to your screen to enter the valid username and password. Enter the username and password which have access into the above subscription. 
 
Now, let's create the Linux VM with PowerShell commands. To start that, we use the existing Resource Group and assign it to the variable $rg.
  1. $rg = Get-AzureRMResourGroup `  
  2.          -Name 'demo-rg' `  
  3.          -Location 'centralus'     
Then, create the Subnet Configuration by executing the below code. This will assign the values into the variable called $subnetconfig. 
  1. $subnetconfig = New-AzureRMVirtualNetworkSubnetConfig `  
  2.                  -Name 'demo-subnet-2' `  
  3.                  -AddressPrefix '10.2.1.0/24'                   
The next step is to create a virtual network with the value created in the above resource group and subnet variables. So, let's see how to create the virtual network below.
  1. $vnet = New-AzureRMVirtualNetowrk `  
  2.        -ResourceGroupName $rg.ResourceGroupName `  
  3.        -Location $rg.Location `  
  4.        -Name 'demo-vnet-2' `  
  5.        -AddressPrefix '10.2.0.0/16' `  
  6.        -Subnet $subnetConfig  
 The above code will create the virtual network and assign the values to the Variable $vnet as well. Now, we have to create the public IP address in the next step.
  1. $pip = New-AzureRmPublicIpAddress '  
  2.      -ResourceGroupName $rg.ResourceGroupName `  
  3.      -Location $rg.Location  
  4.      -Name 'demo-linux-2-pip-1'`  
  5.      -AllocationMethod Static  
This above code creates the public address to the resourcegroup and assigns the values into $pip variable. Now, we have to create the network security group rule in a more granular approach.
  1. $rule1 = New-AzureRmNetworkSecurityRuleConfig `  
  2.        -Name ssh-rule `  
  3.        -Description 'Allow SSH' `  
  4.        -Access Allow `  
  5.        -Protocol TCP`  
  6.        -Direction Inbound`  
  7.        -Prority 100`  
  8.        -SourceAddressPrefix Internet `  
  9.        -SourcePortRange * `  
  10.        -DestinationAddressPrefix *`  
  11.        -DesitnationPortRange 22  
Using the above rule, create a network security group. Here, we are going to apply the above rule defined in the new network security group by executing the below code in PowerShell.
  1. $nsg = NewAzureRmNetworkSecurityGroup '  
  2.    -ResoureGroupName $rg.ResourceGroupName `  
  3.    -Location $rg.Location `  
  4.    -Name 'demo-Linux-nsg-2'`  
  5.    -SecurityRules $rule1  
The next important thing is to create the virtual card and associate with public IP address and Network Security Group (NSG). To do that, first, grab the object representing in our current subnet.  
  1. $subnet = $vnet.subnets | where-Object { $_.Name -eq 'demo-subnet-2' }
  1. $nic = New-AzureRmNetworkInterface `  
  2.     -ResourceGroupName $rg.ResourceGroupName`  
  3.     -Location $rg.Location`  
  4.     -Name 'demo-lin-2-nic-1'`  
  5.     -Subnet $subnet`  
  6.     -PublicIpAddress $pip`  
  7.     -NetworkSecurityGroup $nsg  
So far, we have created the network related configuration and now we have to create the virtual Machine configuration. Execute the below parameters to assign the Name and Password required for the machine we are going to create. 
  1. $LinuxVmConfig = New-AzureRmVMConfig `  
  2.     -VMName 'demo-linux-2'`  
  3.     -VMSize 'Standard_D1'  
Let's assign the Password parameters in a variable before we apply it in actual configuration code. 
  1. $password = ConvertTo-SecureString 'password1234112123$%^&*' -AsPlainText -Force  
  2.   
  3. $LinuxCred = New-Object System.Management.Automation.PSCredential ('demo', $password)  
With the above variables, we are going to set the operating system. 
  1. $LinuxVmConfig = Set-AzureRmVmOperatingSystem `  
  2.      -VM $LinuxVmConfig `  
  3.      -Linux `  
  4.      -ComputerName 'demo-linux-2'`  
  5.      -DisablePasswordAuthentication`  
  6.      -Credential $LinuxCred  
Read in our SSH Keys and add them to VM config as well,
  1. $sshPublicKey = Get-Content "~/.ssh/id_rsa.pub"  
  2.   
  3. Add-AzureRmVMSshPublicKey `  
  4.   -VM $LinuxVmConfig `  
  5.   -KeyData $sshPublicKey `  
  6.   -Path "/home/demoadmin/.ssh/authorized_keys"  
We have to tell which image we are going to use of the operating system we have created above. So, get the VM image name and set it to VM config. In this, we used RHEL/latest. 
  1. Get-AzureRMVMImageSku -Location $rg.Location -PublisherName "Redhat" -Offer "rhel"  
This above script will get the latest package available and we can use the latest to assign it to a variable.
  1. $LinuxVmConfig = Set-AzureRmVMSourceImage`  
  2.    -VM $LinuxVmConfig `  
  3.    -PublisherName 'Redhat'`  
  4.    -Offer 'rhel'`  
  5.    -skus '7.4'`  
  6.    -Version 'latest'  
$LinuxVmConfig holds the value of the operating system and it will be deployed into the VM machine later when we execute it. It's time to add the Created network interface to the VM. 
  1. $LinuxVmConfig = Add-AzureRmVMNetworkInterface `  
  2.      -VM $linuxVmConfig`  
  3.      -Id $nic.Id  
Now is the time to create the actual VM. Here, we are passing the VM configuration, Image, and the network.
  1. New-AzureRmVM `  
  2.   -ResourceGroupName $rg.ResourceGroupName`  
  3.   -Location $rg.Location`  
  4.   -VM $LinuxVmConfig  
Here we go. By running the above code, we actually provisioned the VM on the Azure Portal.
 
For connection to the VM please execute the below commands. 
  1. $MyIP = Get-AzureRmPublicIpAddress `  
  2.    -ResourceGroupName $rg.ResourceGroupName`  
  3.    -Name $pip.Name | Select-Object -ExpandProperty IpAddress  
 To connect our VM via SSH,
  1. SSh -l demoadmin $MyIP  
Once you execute the above code there will be the prompt asking to continue connection. Click yes to continue the connection. 
 
That's it. We have provisioned the Linux VM and established the connection through Powershell Commands. 
 
Thank you for your support!!