Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Create a load-balanced pair of Azure VMs
$testName = "unique-resource-name"
$resourceGroupName = $testName
$location = "northeurope"
$domainName = $testName
$subnetName = "Subnet-1"
$publisher = "MicrosoftDynamicsNAV"
$offer = "DynamicsNAV"
$sku = "2016"
$version = "latest"
$cred = Get-Credential
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
$vip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Name "PublicIP1" `
-Location $location -AllocationMethod Dynamic -DomainNameLabel $domainName
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName `
-AddressPrefix "10.0.64.0/24"
$vnet = New-AzureRmVirtualNetwork -Name "VNET" `
-ResourceGroupName $resourceGroupName `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $subnet
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$feIpConfig = New-AzureRmLoadBalancerFrontendIpConfig -Name $testName -PublicIpAddress $vip
$inboundNatRule1 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name "RDP1" `
-FrontendIpConfiguration $feIpConfig `
-Protocol TCP -FrontendPort 3441 -BackendPort 3389
$inboundNatRule2 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name "RDP2" `
-FrontendIpConfiguration $feIpConfig `
-Protocol TCP -FrontendPort 3442 -BackendPort 3389
$beAddressPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "LBBE"
$healthProbe = New-AzureRmLoadBalancerProbeConfig -Name "HealthProbe" `
-RequestPath "Default.aspx" -Protocol http -Port 80 `
-IntervalInSeconds 15 -ProbeCount 2
$lbrule1 = New-AzureRmLoadBalancerRuleConfig -Name "HTTP" `
-FrontendIpConfiguration $feIpConfig -BackendAddressPool $beAddressPool `
-Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 80
$lbrule2 = New-AzureRmLoadBalancerRuleConfig -Name "HTTPS" `
-FrontendIpConfiguration $feIpConfig -BackendAddressPool $beAddressPool `
-Probe $healthProbe -Protocol Tcp -FrontendPort 443 -BackendPort 443 -LoadDistribution SourceIPProtocol
$lbrule3 = New-AzureRmLoadBalancerRuleConfig -Name "NAV" `
-FrontendIpConfiguration $feIpConfig -BackendAddressPool $beAddressPool `
-Probe $healthProbe -Protocol Tcp -FrontendPort 7046 -BackendPort 7046 -LoadDistribution SourceIPProtocol
$lbrule4 = New-AzureRmLoadBalancerRuleConfig -Name "NAVHELP" `
-FrontendIpConfiguration $feIpConfig -BackendAddressPool $beAddressPool `
-Probe $healthProbe -Protocol Tcp -FrontendPort 49000 -BackendPort 49000
$alb = New-AzureRmLoadBalancer -ResourceGroupName $resourceGroupName `
-Name "ALB" -Location $location -FrontendIpConfiguration $feIpConfig `
-InboundNatRule $inboundNatRule1,$inboundNatRule2 `
-LoadBalancingRule ($lbrule1,$lbrule2,$lbrule3,$lbrule4) -BackendAddressPool $beAddressPool `
-Probe $healthProbe
$nic1 = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName `
-Name "nic1" -Subnet $subnet -Location $location `
-LoadBalancerInboundNatRule $alb.InboundNatRules[0] `
-LoadBalancerBackendAddressPool $alb.BackendAddressPools[0]
$nic2 = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName `
-Name "nic2" -Subnet $subnet -Location $location `
-LoadBalancerInboundNatRule $alb.InboundNatRules[1] `
-LoadBalancerBackendAddressPool $alb.BackendAddressPools[0]
New-AzureRmAvailabilitySet -ResourceGroupName $resourceGroupName `
-Name "AVSet" -Location $location
$avset = Get-AzureRmAvailabilitySet -ResourceGroupName $resourceGroupName -Name "AVSet"
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName `
-Name $testName -Location $location -Type Standard_LRS
Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName
[array]$nics = @($nic1,$nic2)
For ($i=0; $i -le 1; $i++) {
$vmName = "$testName-w$i"
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_DS1" `
-AvailabilitySetId $avSet.Id |
Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName `
-Credential $cred -ProvisionVMAgent -EnableAutoUpdate |
Set-AzureRmVMSourceImage -PublisherName $publisher -Offer $offer -Skus $sku `
-Version $version |
Set-AzureRmVMOSDisk -Name $vmName -VhdUri "https://$testName.blob.core.windows.net/vhds/$vmName-os.vhd" `
-Caching ReadWrite -CreateOption fromImage |
Add-AzureRmVMNetworkInterface -Id $nics[$i].Id
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location `
-VM $vmConfig
}
$ipAddr = Get-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName
$ipAddr.IpAddress
$ipAddr.DnsSettings.Fqdn
Find-AzureRmResource -ResourceGroupNameContains $resourceGroupName | Select Name, ResourceType
<#
Remove-AzureRmResourceGroup -Name $resourceGroupName
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment