Skip to content

Instantly share code, notes, and snippets.

@albal
Created March 27, 2024 14:52
Show Gist options
  • Select an option

  • Save albal/872846159f6d211ff4613ee1d3455413 to your computer and use it in GitHub Desktop.

Select an option

Save albal/872846159f6d211ff4613ee1d3455413 to your computer and use it in GitHub Desktop.
Create two VMs in different Azure Regions using Az PowerShell
# Define variables
$username = "kodekloud" # Username for the VM
$plainPassword = "VMP@55w0rd" # Your VM password
$vmSize = "Standard_B1s"
$azureVmPublisherName = "Canonical"
$azureVmOffer = "0001-com-ubuntu-server-jammy"
$azureVmSkus = "22_04-LTS"
# Creating VM credential; use your own password and username by changing the variables
$password = ConvertTo-SecureString $plainPassword -AsPlainText -Force
$vmCredential = New-Object System.Management.Automation.PSCredential ($username, $password)
# Get resource group
$resourceGroupName = (Get-AzResourceGroup).ResourceGroupName
# Configuration for each location
$locations = @('EastUS', 'WestUS')
foreach ($location in $locations) {
$locationPrefix = $location -replace 'US', ''
$subnetName = "MySubnet-$locationPrefix"
$nicName = "MyNIC-$locationPrefix"
$networkName = "MyNet-$locationPrefix"
$publicIpName = "MyPublicIP-$locationPrefix"
$nsgName = "NSG-$locationPrefix"
$vmName = $location
# Convert the first letter of location to its ASCII numeric digit
$subnet_value = [System.Text.Encoding]::ASCII.GetBytes($location.Substring(0, 1))[0].ToString()
$subnetAddressPrefix = "10.$subnet_value.0.0/24"
$vnetAddressPrefix = "10.$subnet_value.0.0/16"
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name RDP -Protocol Tcp -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22 -Access Allow
$nsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName -Location $location -Name $nsgName -SecurityRules $nsgRuleSSH
$publicIp = New-AzPublicIpAddress -Name $publicIpName -ResourceGroupName $resourceGroupName -Location $location -AllocationMethod Static
$singleSubnet = New-AzVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix $subnetAddressPrefix
$vnet = New-AzVirtualNetwork -Name $networkName -ResourceGroupName $resourceGroupName -Location $location -AddressPrefix $vnetAddressPrefix -Subnet $singleSubnet
$nic = New-AzNetworkInterface -Name $nicName -ResourceGroupName $resourceGroupName -Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id -NetworkSecurityGroupId $nsg.Id
$virtualMachine = New-AzVMConfig -VMName $vmName -VMSize $vmSize
$virtualMachine = Set-AzVMOperatingSystem -Linux -VM $virtualMachine -ComputerName $vmName -Credential $vmCredential
$virtualMachine = Add-AzVMNetworkInterface -VM $virtualMachine -Id $nic.Id
$virtualMachine = Set-AzVMSourceImage -VM $virtualMachine -PublisherName $azureVmPublisherName -Offer $azureVmOffer -Skus $azureVmSkus -Version "latest"
$virtualMachine = Set-AzVMBootDiagnostic -VM $virtualMachine -Disable
$virtualMachine = Set-AzVMOSDisk -VM $virtualMachine -StorageAccountType "StandardSSD_LRS" -CreateOption FromImage
New-AzVM -ResourceGroupName $resourceGroupName -Location $location -VM $virtualMachine -Verbose
}
Start-Sleep -Seconds 60 # Wait for IPs to be assigned
# Retrieve IP Addresses
$locations | ForEach-Object {
$locationPrefix = $_ -replace 'US', ''
$publicIpName = "MyPublicIP-$locationPrefix"
Write-Host "$locationPrefix IP: ", (Get-AzPublicIpAddress -Name $publicIpName).IpAddress
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment