AzureILBUpgrade.ps1
<#PSScriptInfo .VERSION 3.0 .GUID 836ca1ab-93b7-49a3-b1d1-b257601da1dd .AUTHOR Microsoft Corporation .COMPANYNAME Microsoft Corporation .COPYRIGHT Microsoft Corporation. All rights reserved. .TAGS Azure, Az, LoadBalancer, AzNetworking .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION This script will help you create a Standard SKU Internal load balancer with the same configuration as your Basic SKU load balancer. .PARAMETER rgName Name of ResourceGroup of Basic Internal Load Balancer and the Standard Internal Load Balancer, like "microsoft_rg1" .PARAMETER oldLBName Name of Basic Internal Load Balancer you want to upgrade. .PARAMETER newlocation Location where you want to place new Standard Internal Load Balancer in. For example, "centralus" .PARAMETER newLBName Name of the newly created Standard Internal Load Balancer. .EXAMPLE ./AzureILBUpgrade.ps1 -rgName "test_InternalUpgrade_rg" -oldLBName "LBForInternal" -newlocation "centralus" -newLbName "LBForUpgrade" .LINK https://aka.ms/upgradeloadbalancerdoc https://docs.microsoft.com/en-us/azure/load-balancer/load-balancer-overview/ .NOTES Note - all paramemters are required in order to successfully create a Standard Internal Load Balancer. #> Param( [Parameter(Mandatory = $True)][string] $rgName, [Parameter(Mandatory = $True)][string] $oldLBName, #Parameters for new Standard Load Balancer [Parameter(Mandatory = $True)][string] $newlocation, [Parameter(Mandatory = $True)][string] $newLBName ) #getting current loadbalancer $lb = Get-AzLoadBalancer -ResourceGroupName $rgName -Name $oldLbName #$originalIP = $lb.FrontendIpConfigurations[0].PrivateIpAddress #1. Backend subnet is always the same as the front end ip config - automatic association $vnetName = ($lb.FrontendIpConfigurations.subnet.id).Split("/")[8] $vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgName $backendSubnetName = $lb.FrontendIpConfigurations.subnet.id.Split("/")[10] $backendSubnet = Get-AzVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet $ipRange = ($backendSubnet.AddressPrefix).split("/")[0] $newlbFrontendConfigs = $lb.FrontendIpConfigurations $feProcessed = 1 $startIp = $ipRange.Split(".")[3] + 1 $startIPTest = $ipRange.Split(".")[0] + "." + $ipRange.Split(".")[1] + "." + $ipRange.Split(".")[2] + "." + $startIp $availableIPS = (Test-AzPrivateIPAddressAvailability -VirtualNetwork $vnet -IPAddress $startIPTest).AvailableIPAddresses $lastAvailableIp = $availableIPS[$availableIPS.count-1] #initial bit in array to check for available ips $i = 0 #creating array to store original ips $frontEndArray=@() #2. Front Ends foreach ($frontEndConfig in $newlbFrontendConfigs) { Get-AzLoadBalancerFrontendIpConfig -Name ($frontEndConfig).Name -LoadBalancer $lb $newFrontEndConfigName = $frontEndConfig.Name ##adding information to array $frontEndArray += $newFrontEndConfigName + "," + $frontEndConfig.PrivateIpAddress $newFrontEndIp = $availableIPS[$i] #$newFrontEndIp = $frontEndConfig.PrivateIpAddress $newSubnetId = $frontEndConfig.subnet.Id #2. create frontend config New-Variable -Name "frontEndIpConfig$feProcessed" -Value (New-AzLoadBalancerFrontendIpConfig -Name $newFrontEndConfigName -PrivateIpAddress $newFrontEndIp -SubnetId $newSubnetId) $feProcessed++ $i++ } $rulesFrontEndIpConfig = (Get-Variable -Include frontEndIpConfig*) #3. create inbound nat rule configs $newlbNatRules = $lb.InboundNatRules ##looping through NAT Rules $ruleprocessed = 1 foreach ($natRule in $newlbNatRules) { ##need to get correct frontend IP Config $frontEndName = (($natRule.FrontendIPConfiguration).id).Split("/")[10] $frontEndNameConfig = ((Get-Variable -Include frontEndIpConfig* | Where-Object {$_.Value.name -eq $frontEndName})).value New-Variable -Name "nat$ruleprocessed" -Value (New-AzLoadBalancerInboundNatRuleConfig -Name $natRule.name -FrontendIpConfiguration $frontEndNameConfig -Protocol $natRule.Protocol -FrontendPort $natRule.FrontendPort -BackendPort $natRule.BackendPort) $ruleprocessed++ } $rulesNat = (Get-Variable -Include nat* | Where-Object {$_.Name -ne "natRule"}) ##setting private ip to last available ip address - cannot be null ##adding a loop $z = 0 $y = 1 foreach ($item in $newlbFrontendConfigs) { $item.PrivateIpAddress = $availableIPS[$availableIPS.count-$y] $lb | Set-AzLoadBalancer $y++ $z++ } #4. Create loadbalancer $newlb = New-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName -SKU Standard -Location $newlocation -FrontendIpConfiguration $rulesFrontEndIpConfig.Value -InboundNatRule $rulesNat.Value #-outboundRule $outboundrule #getting LB now after creation $newlb = (Get-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName) #5. Probe configuration $newProbes = Get-AzLoadBalancerProbeConfig -LoadBalancer $lb foreach ($probe in $newProbes) { $probeName = $probe.name $probeProtocol = $probe.protocol $probePort = $probe.port $probeInterval = $probe.intervalinseconds $probeRequestPath = $probe.requestPath $probeNumbers = $probe.numberofprobes $newlb | Add-AzLoadBalancerProbeConfig -Name $probeName -RequestPath $probeRequestPath -Protocol $probeProtocol -Port $probePort -IntervalInSeconds $probeInterval -ProbeCount $probeNumbers $newlb | Set-AzLoadBalancer } #6. Backend configuration $backendArray=@() $newBackendPools = $lb.BackendAddressPools $newlb = (Get-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName) foreach ($newBackendPool in $newBackendPools) { $existingBackendPoolConfig = Get-AzLoadBalancerBackendAddressPoolConfig -LoadBalancer $lb -Name ($newBackendPool).Name $newlb | Add-AzLoadBalancerBackendAddressPoolConfig -Name ($existingBackendPoolConfig).Name | Set-AzLoadBalancer $newlb = (Get-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName) #$newBackendPoolConfig $nics = (($lb.BackendAddressPools) | Where-Object {$_.Name -eq ($newBackendPool).name}).backendipconfigurations foreach ($nic in $nics) { $nicToAdd = Get-AzNetworkInterface -name ($nic.id).Split("/")[8] -ResourceGroupName $rgName #write-host "Reconfiguring $nicToAdd.Name" $nicToAdd.IpConfigurations[0].LoadBalancerBackendAddressPools = $null Set-AzNetworkInterface -NetworkInterface $nicToAdd $backendArray += ($newBackendPool).Name +"," + ($nicToAdd).id } } #7. Re-adding NICs to backend pool foreach ($backendItem in $backendArray) { $newlb = (Get-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName) #write-host "backend" $lbBackend = Get-AzLoadBalancerBackendAddressPoolConfig -name ($backendItem.Split(",")[0]) -LoadBalancer $newlb #$lbBackend #write-host "nic" $nicToAssociate = Get-AzNetworkInterface -name (($backendItem.Split(",")[1]).split("/")[8]) -resourcegroupname $rgName #$nicToAssociate $nicToAssociate.IpConfigurations[0].LoadBalancerBackendAddressPools = $lbBackend Set-AzNetworkInterface -NetworkInterface $nicToAssociate } #8. create load balancer rule config $newlb = (Get-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName) $newLbRuleConfigs = Get-AzLoadBalancerRuleConfig -LoadBalancer $lb foreach ($newLbRuleConfig in $newLbRuleConfigs) { $backendPool = (Get-AzLoadBalancerBackendAddressPoolConfig -LoadBalancer $newlb -Name ((($newLbRuleConfig.BackendAddressPool.id).split("/"))[10])) $lbFrontEndName = (($newLbRuleConfig.FrontendIPConfiguration).id).Split("/")[10] $lbFrontEndNameConfig = ((Get-Variable -Include frontEndIpConfig* | Where-Object {$_.Value.name -eq $lbFrontEndName})).value $newlb | Add-AzLoadBalancerRuleConfig -Name ($newLbRuleConfig).Name -FrontendIPConfiguration $lbFrontEndNameConfig -BackendAddressPool $backendPool -Probe (Get-AzLoadBalancerProbeConfig -LoadBalancer $newlb -Name (($newLbRuleConfig.Probe.id).split("/")[10])) -Protocol ($newLbRuleConfig).protocol -FrontendPort ($newLbRuleConfig).FrontendPort -BackendPort ($newLbRuleConfig).BackendPort -IdleTimeoutInMinutes ($newLbRuleConfig).IdleTimeoutInMinutes -EnableFloatingIP -LoadDistribution SourceIP -DisableOutboundSNAT $newlb | set-AzLoadBalancer foreach ($backendIpConfig in $backendPool.BackendIpConfigurations) { #$backendIpConfig $nicToAssociate = Get-AzNetworkInterface -name (($backendIpConfig.id).split("/")[8]) -ResourceGroupName $rgName #$nicToAssociate $nicToAssociate.IpConfigurations[0].LoadBalancerBackendAddressPools = $lbBackend Set-AzNetworkInterface -NetworkInterface $nicToAssociate } } #9. re-adding private ip addresses foreach ($frontendItem in $frontEndArray) { $newlb = (Get-AzLoadBalancer -ResourceGroupName $rgName -Name $newLbName) #$frontEndName $lbFrontEnd = Get-AzLoadBalancerFrontendIpConfig -LoadBalancer $newlb -Name ($frontendItem.Split(",")[0]) $lbFrontEnd.PrivateIpAddress = ($frontEndItem.Split(",")[1]) $newlb | set-AzLoadBalancer } |