Skip to content

Instantly share code, notes, and snippets.

@albal
Created March 26, 2024 21:56
Show Gist options
  • Select an option

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

Select an option

Save albal/f00100de34927ff3c974636578731e29 to your computer and use it in GitHub Desktop.
Creation of two Ubuntu VMs in Azure East and West US
# Define variables
$username = "kodekloud" #username for the VM
$plainPassword = "VMP@55w0rd" #your VM password
$vmSize = "Standard_B1s"
#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);
$azureVmPublisherName="Canonical"
$azureVmOffer="0001-com-ubuntu-server-jammy"
$azureVmSkus="22_04-LTS"
# Get resource group
$resourceGroupName = (Get-AzResourceGroup).ResourceGroupName
$EastLocation = "EastUS"
$EastVmName = "EastUS"
$EastNetworkName = "MyNet-East"
$EastNICName = "MyNIC-East"
$EastSubnetName = "MySubnet-East"
$EastPublicIpName = "MyPublicIP-East"
$EastSubnetAddressPrefix = "10.0.0.0/24"
$EastVnetAddressPrefix = "10.0.0.0/16"
$WestLocation = "WestUS"
$WestVmName = "WestUS"
$WestNetworkName = "MyNet-West"
$WestNICName = "MyNIC-West"
$WestSubnetName = "MySubnet-West"
$WestPublicIpName = "MyPublicIP-West"
$WestSubnetAddressPrefix = "10.10.0.0/24"
$WestVnetAddressPrefix = "10.10.0.0/16"
$nsgRuleSSH = New-AzNetworkSecurityRuleConfig -Name RDP -Protocol Tcp -Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22 -Access Allow;
$EastNsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName -Location $EastLocation -Name EastNSG -SecurityRules $nsgRuleSSH;
$WestNsg = New-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName -Location $WestLocation -Name WestNSG -SecurityRules $nsgRuleSSH;
$EastPublicIp = New-AzPublicIpAddress -Name $EastPublicIpName -ResourceGroupName $resourceGroupName -Location $EastLocation -AllocationMethod Static
$EastSingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $EastSubnetName -AddressPrefix $EastSubnetAddressPrefix
$EastVnet = New-AzVirtualNetwork -Name $EastNetworkName -ResourceGroupName $resourceGroupName -Location $EastLocation -AddressPrefix $EastVnetAddressPrefix -Subnet $EastSingleSubnet
$EastNIC = New-AzNetworkInterface -Name $EastNICName -ResourceGroupName $resourceGroupName -Location $EastLocation -SubnetId $EastVnet.Subnets[0].Id -PublicIpAddressId $EastPublicIp.Id -NetworkSecurityGroupId $EastNsg.Id
$EastVirtualMachine = New-AzVMConfig -VMName $EastVmName -VMSize $VmSize
$EastVirtualMachine = Set-AzVMOperatingSystem -Linux -VM $EastVirtualMachine -ComputerName $EastVmName -Credential $vmCredential
$EastVirtualMachine = Add-AzVMNetworkInterface -VM $EastVirtualMachine -Id $EastNIC.Id
$EastVirtualMachine = Set-AzVMSourceImage -VM $EastVirtualMachine -PublisherName $azureVmPublisherName -Offer $azureVmOffer -Skus $azureVmSkus -Version "latest"
$EastVirtualMachine = Set-AzVMBootDiagnostic -VM $EastVirtualMachine -Disable
$EastVirtualMachine = Set-AzVMOSDisk -VM $EastVirtualMachine -StorageAccountType "StandardSSD_LRS" -CreateOption FromImage
$WestPublicIp = New-AzPublicIpAddress -Name $WestPublicIpName -ResourceGroupName $resourceGroupName -Location $WestLocation -AllocationMethod Static
$WestSingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $WestSubnetName -AddressPrefix $WestSubnetAddressPrefix
$WestVnet = New-AzVirtualNetwork -Name $WestNetworkName -ResourceGroupName $resourceGroupName -Location $WestLocation -AddressPrefix $WestVnetAddressPrefix -Subnet $WestSingleSubnet
$WestNIC = New-AzNetworkInterface -Name $WestNICName -ResourceGroupName $resourceGroupName -Location $WestLocation -SubnetId $WestVnet.Subnets[0].Id -PublicIpAddressId $WestPublicIp.Id -NetworkSecurityGroupId $WestNsg.Id
$WestVirtualMachine = New-AzVMConfig -VMName $WestVmName -VMSize $VmSize
$WestVirtualMachine = Set-AzVMOperatingSystem -Linux -VM $WestVirtualMachine -ComputerName $WestVmName -Credential $vmCredential
$WestVirtualMachine = Add-AzVMNetworkInterface -VM $WestVirtualMachine -Id $WestNIC.Id
$WestVirtualMachine = Set-AzVMSourceImage -VM $WestVirtualMachine -PublisherName $azureVmPublisherName -Offer $azureVmOffer -Skus $azureVmSkus -Version "latest"
$WestVirtualMachine = Set-AzVMBootDiagnostic -VM $WestVirtualMachine -Disable
$WestVirtualMachine = Set-AzVMOSDisk -VM $WestVirtualMachine -StorageAccountType "StandardSSD_LRS" -CreateOption FromImage
New-AzVM -ResourceGroupName $resourceGroupName -Location $EastLocation -VM $EastVirtualMachine -Verbose
New-AzVM -ResourceGroupName $resourceGroupName -Location $WestLocation -VM $WestVirtualMachine -Verbose
(Get-AzPublicIpAddress -Name $EastPublicIpName).IpAddress
(Get-AzPublicIpAddress -Name $WestPublicIpName).IpAddress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment