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.
- 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.
- 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.
- $rg = Get-AzureRMResourGroup `
- -Name 'demo-rg' `
- -Location 'centralus'
Then, create the Subnet Configuration by executing the below code. This will assign the values into the variable called $subnetconfig.
- $subnetconfig = New-AzureRMVirtualNetworkSubnetConfig `
- -Name 'demo-subnet-2' `
- -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.
- $vnet = New-AzureRMVirtualNetowrk `
- -ResourceGroupName $rg.ResourceGroupName `
- -Location $rg.Location `
- -Name 'demo-vnet-2' `
- -AddressPrefix '10.2.0.0/16' `
- -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.
- $pip = New-AzureRmPublicIpAddress '
- -ResourceGroupName $rg.ResourceGroupName `
- -Location $rg.Location
- -Name 'demo-linux-2-pip-1'`
- -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.
- $rule1 = New-AzureRmNetworkSecurityRuleConfig `
- -Name ssh-rule `
- -Description 'Allow SSH' `
- -Access Allow `
- -Protocol TCP`
- -Direction Inbound`
- -Prority 100`
- -SourceAddressPrefix Internet `
- -SourcePortRange * `
- -DestinationAddressPrefix *`
- -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.
- $nsg = NewAzureRmNetworkSecurityGroup '
- -ResoureGroupName $rg.ResourceGroupName `
- -Location $rg.Location `
- -Name 'demo-Linux-nsg-2'`
- -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.
- $subnet = $vnet.subnets | where-Object { $_.Name -eq 'demo-subnet-2' }
- $nic = New-AzureRmNetworkInterface `
- -ResourceGroupName $rg.ResourceGroupName`
- -Location $rg.Location`
- -Name 'demo-lin-2-nic-1'`
- -Subnet $subnet`
- -PublicIpAddress $pip`
- -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.
- $LinuxVmConfig = New-AzureRmVMConfig `
- -VMName 'demo-linux-2'`
- -VMSize 'Standard_D1'
Let's assign the Password parameters in a variable before we apply it in actual configuration code.
- $password = ConvertTo-SecureString 'password1234112123$%^&*' -AsPlainText -Force
-
- $LinuxCred = New-Object System.Management.Automation.PSCredential ('demo', $password)
With the above variables, we are going to set the operating system.
- $LinuxVmConfig = Set-AzureRmVmOperatingSystem `
- -VM $LinuxVmConfig `
- -Linux `
- -ComputerName 'demo-linux-2'`
- -DisablePasswordAuthentication`
- -Credential $LinuxCred
Read in our SSH Keys and add them to VM config as well,
- $sshPublicKey = Get-Content "~/.ssh/id_rsa.pub"
-
- Add-AzureRmVMSshPublicKey `
- -VM $LinuxVmConfig `
- -KeyData $sshPublicKey `
- -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.
- 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.
- $LinuxVmConfig = Set-AzureRmVMSourceImage`
- -VM $LinuxVmConfig `
- -PublisherName 'Redhat'`
- -Offer 'rhel'`
- -skus '7.4'`
- -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.
- $LinuxVmConfig = Add-AzureRmVMNetworkInterface `
- -VM $linuxVmConfig`
- -Id $nic.Id
Now is the time to create the actual VM. Here, we are passing the VM configuration, Image, and the network.
- New-AzureRmVM `
- -ResourceGroupName $rg.ResourceGroupName`
- -Location $rg.Location`
- -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.
- $MyIP = Get-AzureRmPublicIpAddress `
- -ResourceGroupName $rg.ResourceGroupName`
- -Name $pip.Name | Select-Object -ExpandProperty IpAddress
To connect our VM via SSH,
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!!