Author : MD TAREQ HASSAN | Updated : 2020/11/09
Prerequisites
Create required resources beforehand to use those during VM creation. If not created beforehand, required resources will be created during VM creation.
Create Resource Group:
- Go to: https://portal.azure.com/#create/Microsoft.ResourceGroup (login to Azure portal first, then click the link)
- Fillup details (i.e. name ‘
demo-rg
’, select subscription etc.) - Create
Create required resources:
- Create Network Security Group
- Create VNet and subnet
- Create public IP address
- Create network interface (NIC)
- Associate public IP address to NIC
- Create storage account for VM diagnostics
AZ module installation
- Install Azure PowerShell module: https://docs.microsoft.com/en-us/powershell/azure/install-az-ps
- Or open PowerShell as admin and execute command:
Install-Module Az
Create Virtual Machine
- Either use PowerShell ISE in your computer or use Azure Cloud Shell
- When using PowerShell ISE:
- Run as Admin
- Login to Azure first
- Execute commands
- You can paste all commands and then run line by line (select and run)
- If Azure command is not recognized:
Import-Module Az
- If still does not work => use Cloud Shell instead
- When using Cloud Shell: select PowerShell (instead of Bash)
- About
Get-Credential
command:- Will ask for VM user name and password
- VM user name can not be more than 20 chars
- It will take some time to create resources
- Note: Azure VM images come with 128GB OS disk, to change size of OS disk, create custom image or find an image in the marketplace that uses smaller/larger OS disk size
Login to Azure when using local PowerShell or PowerShell ISE
Login-AzAccount # Login-AzureRmAccount
Connect-AzAccount
Commands to create VM (Cloud Shell is preferred)
# 1
Get-AzSubscription # Get-AzureRmSubscription
# 2
Set-AzContext -SubscriptionName "Free Trial" # Set-AzureRmContext
# 3
$location = "japaneast"
# 4
$rgName = "IaaS-ps-rg"
# 5
$credential = Get-Credential # set id & password for VM
# 6
New-AzResourceGroup -Name $rgName -Location $location # New-AzureRmResourceGroup
# 7
New-AzVm `
-ResourceGroupName $rgName `
-Name "IaaS-ps" `
-Location $location `
-VirtualNetworkName "IaaS-ps-vnet" `
-SubnetName "IaaS-ps-subnet" `
-SecurityGroupName "IaaS-ps-nsg" `
-PublicIpAddressName "IaaS-ps-ip" `
-OpenPorts 80,3389 `
-Credential $credential # New-AzureRmVm
# 8
$ipAddress = Get-AzPublicIpAddress -ResourceGroupName Iaas-ps-rg `
| Select-Object -ExpandProperty IpAddress # Get-AzureRmPublicIpAddress
# 9
$ipAddress # just to see ip address of the created vm