How To Create An Azure Virtual Machine Using PowerShell
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In this blog post, we will discuss how to create an Azure Virtual Machine using PowerShell.
What Is PowerShell:
PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language.
What is Azure Virtual Machine:
Azure Virtual Machine or Azure VM is the compute service offered by Azure to host your virtual servers in Azure cloud. Azure Virtual Machine is basically a virtual server running on Azure datacenter. Like in AWS you use EC2 instance or in GCP you use Google Compute Engine Instance, in Azure, you will use Azure VM instance if you need to create and manage your own server on Azure cloud.
Prerequisites:
- One system with PowerShell for Azure installed and configured.
You can follow the below blog post if you haven’t installed and configured PowerShell for Azure yet.
https://cloudaffaire.com/how-to-install-and-configure-powershell-for-azure/
How To Create An Azure Virtual Machine Using PowerShell:
Step 1: Select the Azure location of your choice where you will deploy your Azure VM. For this demo, I am using southeastasia location.
1 2 3 4 5 6 |
############################################################# ## How To Create An Azure Virtual Machine Using PowerShell ## ############################################################# ## Get Azure location code and select a location to host your VM Get-AzLocation | Select-Object DisplayName,Location; |
Step 2: Create a resource group.
1 2 3 4 |
## Create a resource group New-AzResourceGroup ` -Name "MyResourceGroup" ` -Location "southeastasia"; |
Step 3: Create a virtual network.
1 2 3 4 5 6 7 8 9 10 11 |
## Create a virtual network $SubnetConfig = New-AzVirtualNetworkSubnetConfig ` -Name "MySubNetwork" ` -AddressPrefix 10.0.1.0/24; $MyNetwork = New-AzVirtualNetwork ` -ResourceGroupName "MyResourceGroup" ` -Location "southeastasia" ` -Name "MyNetwork" ` -AddressPrefix "10.0.0.0/16" ` -Subnet $SubnetConfig; |
Step 4: Create a public IP address.
1 2 3 4 5 6 7 |
## Create a public IP address and specify a DNS name $MyPublicIP = New-AzPublicIpAddress ` -ResourceGroupName "MyResourceGroup" ` -Location "southeastasia" ` -Name "MyPublicIP" ` -AllocationMethod Static ` -IdleTimeoutInMinutes 4; |
Step 5: Create a network security group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## Create an inbound network security group rule for port 22 $MynsgRuleSSH = New-AzNetworkSecurityRuleConfig ` -Name "MyNetworkSecurityGroupRuleSSH" ` -Protocol Tcp ` -Direction Inbound ` -Priority 1000 ` -SourceAddressPrefix * ` -SourcePortRange * ` -DestinationAddressPrefix * ` -DestinationPortRange 22 ` -Access Allow; ## Create a network security group $Mynsg = New-AzNetworkSecurityGroup ` -ResourceGroupName "MyResourceGroup" ` -Location "southeastasia" ` -Name "MyNetworkSecurityGroup" ` -SecurityRules $MynsgRuleSSH; |
Step 6: Create a virtual network card with the public IP address and network security group.
1 2 3 4 5 6 7 8 |
## Create a virtual network card and associate with public IP address and NSG $Mynic = New-AzNetworkInterface ` -Name "MyNic" ` -ResourceGroupName "MyResourceGroup" ` -Location "southeastasia" ` -SubnetId $MyNetwork.Subnets[0].Id ` -PublicIpAddressId $MyPublicIP.Id ` -NetworkSecurityGroupId $Mynsg.Id; |
Step 7: Define a username with a blank password for your Azure VM.
1 2 3 |
## Define user name and blank password $securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force; $cred = New-Object System.Management.Automation.PSCredential ("azureuser", $securePassword); |
Step 8: Create SSH key’s for your Azure VM.
1 2 3 4 |
## Configure SSH Keys ssh-keygen.exe ## generate ssh key if not already exist $sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub" Add-AzVMSshPublicKey -VM $vmconfig -KeyData $sshPublicKey -Path "/home/azureuser/.ssh/authorized_keys" |
Step 9: Define your Azure VM configuration. For this demo, I have used a CentOS image for my OS.
1 2 3 4 5 6 7 8 9 10 |
## Create a virtual machine configuration $vmConfig = New-AzVMConfig -VMName "MyAzureVM" -VMSize Standard_B1s | Set-AzVMOperatingSystem -Linux -ComputerName "MyAzureVM" -Credential $cred -DisablePasswordAuthentication | Set-AzVMSourceImage -PublisherName "OpenLogic" -Offer "CentOS" -Skus "7.5" -Version "7.5.201808150" | ## (CentOS 7) ## Set-AzVMSourceImage -PublisherName "RedHat" -Offer "RHEL" -Skus "7-LVM" -Version "7.9.2020100116" | ## (RHEL 7) ## Set-AzVMSourceImage -PublisherName "Canonical" -Offer "UbuntuServer" -Skus "18.04-LTS" -Version "18.04.202010140" | ## (Ubuntu 18) ## Set-AzVMSourceImage -PublisherName "Debian" -Offer "debian-10" -Skus "10" -Version "0.20201023.432" | ## (Debian 10) ## Set-AzVMSourceImage -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2019-Datacenter" -Version "2019.0.20190410" | (WindowsServer 2019) Add-AzVMNetworkInterface -Id $Mynic.Id; |
Step 10: Create your Azure VM.
1 2 3 4 5 |
## Create a virtual machine New-AzVM ` -ResourceGroupName "MyResourceGroup" ` -Location "southeastasia" ` -VM $vmConfig; |
Step 11: Get the public IP address of your Azure VM.
1 2 3 |
## Get public IP address for your Azure VM Get-AzPublicIpAddress ` -ResourceGroupName "MyResourceGroup" | Select-Object IpAddress |
Step 12: Connect to your Azure VM.
1 2 3 |
## Connect to your Azure VM ssh -i \.ssh\id_rsa azureuser@ exit |
Step 13: Cleanup
1 2 |
## Cleanup Remove-AzResourceGroup -Name "MyResourceGroup"; |
Hope you have enjoyed this blog post. Please refer below Azure documentation for more details
https://docs.microsoft.com/en-us/powershell/module/?view=azps-4.8.0
https://docs.microsoft.com/en-us/azure/virtual-machines/