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'
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'
- $subnetconfig = New-AzureRMVirtualNetworkSubnetConfig `
- -Name 'demo-subnet-2' `
- -AddressPrefix '10.2.1.0/24'
- $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
- $rule1 = New-AzureRmNetworkSecurityRuleConfig `
- -Name ssh-rule `
- -Description 'Allow SSH' `
- -Access Allow `
- -Protocol TCP`
- -Direction Inbound`
- -Prority 100`
- -SourceAddressPrefix Internet `
- -SourcePortRange * `
- -DestinationAddressPrefix *`
- -DesitnationPortRange 22
- $nsg = NewAzureRmNetworkSecurityGroup '
- -ResoureGroupName $rg.ResourceGroupName `
- -Location $rg.Location `
- -Name 'demo-Linux-nsg-2'`
- -SecurityRules $rule1
- $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
- $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)
- $LinuxVmConfig = Set-AzureRmVmOperatingSystem `
- -VM $LinuxVmConfig `
- -Linux `
- -ComputerName 'demo-linux-2'`
- -DisablePasswordAuthentication`
- -Credential $LinuxCred
- $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"
- $LinuxVmConfig = Set-AzureRmVMSourceImage`
- -VM $LinuxVmConfig `
- -PublisherName 'Redhat'`
- -Offer 'rhel'`
- -skus '7.4'`
- -Version 'latest'
- $LinuxVmConfig = Add-AzureRmVMNetworkInterface `
- -VM $linuxVmConfig`
- -Id $nic.Id
- New-AzureRmVM `
- -ResourceGroupName $rg.ResourceGroupName`
- -Location $rg.Location`
- -VM $LinuxVmConfig
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,
- SSh -l demoadmin $MyIP
That's it. We have provisioned the Linux VM and established the connection through Powershell Commands.
Thank you for your support!!

Join the conversation! Your thoughts help the community grow.