.VERSION 5.0.2
.GUID 836ca1ab-93b7-49a3-b1d1-b257601da1dd
.AUTHOR Microsoft
.COPYRIGHT Microsoft 2023
Removing unused variable

This script will help you create a Standard SKU Internal load balancer with the same configuration as your Basic SKU load balancer.
Name of ResourceGroup of Basic Internal Load Balancer and the Standard Internal Load Balancer, like "microsoft_rg1"
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"
Name of the newly created Standard Internal Load Balancer.
./AzureILBUpgrade.ps1 -rgName "test_InternalUpgrade_rg" -oldLBName "LBForInternal" -newlocation "centralus" -newLbName "LBForUpgrade"
Note - all paramemters are required in order to successfully create a Standard Internal Load Balancer.
[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 = ($"/")[8]
$vnetRGName = ($"/")[4]
$vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $vnetRGName
$backendSubnetName = $"/")[10]
$backendSubnet = Get-AzVirtualNetworkSubnetConfig -Name $backendSubnetName -VirtualNetwork $vnet
$ipRange = ($backendSubnet.AddressPrefix).split("/")[0]

$newlbFrontendConfigs = $lb.FrontendIpConfigurations
$feProcessed = 1

[int]$startIp = [int]$ipRange.Split(".")[3] + 1
$startIPTest = $ipRange.Split(".")[0] + "." + $ipRange.Split(".")[1] + "." + $ipRange.Split(".")[2] + "." + $startIp

$availableIPS = (Test-AzPrivateIPAddressAvailability -VirtualNetwork $vnet -IPAddress $startIPTest).AvailableIPAddresses
#initial bit in array to check for available ips
$i = 0

#creating array to store original ips

#2. Front Ends
foreach ($frontEndConfig in $newlbFrontendConfigs)
    Get-AzLoadBalancerFrontendIpConfig -Name ($frontEndConfig).Name -LoadBalancer $lb
    $newFrontEndConfigName = $frontEndConfig.Name
    $newSubnetId = $frontEndConfig.subnet.Id
    $ip = $frontEndConfig.PrivateIpAddress

    ##adding information to array
    $frontEndArray += $newFrontEndConfigName + "," + $frontEndConfig.PrivateIpAddress

    ##Creating variables with original IP information to be used with new load balancer creation
    New-Variable -Name "frontEndIpConfig$feProcessed" -Value (New-AzLoadBalancerFrontendIpConfig -Name $newFrontEndConfigName -PrivateIpAddress $ip -SubnetId $newSubnetId)
    #$newFrontEndIp = $availableIPS[$i]

    ## Increment Counters in loop

$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 front end ip config
    $frontEndName = (($natRule.FrontendIPConfiguration).id).Split("/")[10]
    # $frontEndNameConfig = $rulesFrontEndIpConfig| Where-Object {$_.Value.Name -eq $frontEndName}
    $frontEndNameConfig = ((Get-Variable -Include frontEndIpConfig* | Where-Object {$ -eq $frontEndName})).value
    New-Variable -Name "nat$ruleprocessed" -Value (New-AzLoadBalancerInboundNatRuleConfig -Name $ -FrontendIpConfiguration $frontEndNameConfig -Protocol $natRule.Protocol -FrontendPort $natRule.FrontendPort -BackendPort $natRule.BackendPort)
$rulesNat = (Get-Variable -Include nat* | Where-Object {$_.Name -ne "natRule"})

##updating private ip address to new values
##adding a loop
$y = 1
for ($z = 0; $z -lt $newlbFrontendConfigs.count; $z++)
    $ip = $availableIPS[$availableIPS.count-$y]

    $lb | Set-AzLoadBalancerFrontendIpConfig -name $newlbFrontendConfigs[$z].Name -PrivateIpAddress $ip -subnet $newlbFrontendConfigs[$z].subnet


$lb | Set-AzLoadBalancer

# ##update BASIC loadbalancer to reflect newly assigned IP values
# $lb | Set-AzLoadBalancer -FrontendIpConfiguration $newlbFrontendConfigs

#4. Create loadbalancer - this uses the original IP values from the basic Load Balancer
$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 = $
    $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
$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)
    $nics = (($lb.BackendAddressPools) | Where-Object {$_.Name -eq ($newBackendPool).name}).backendipconfigurations
    foreach ($nic in $nics)
        $nicRG = $"/")[4]
        $nicToAdd = Get-AzNetworkInterface -name ($"/")[8] -ResourceGroupName $nicRG
        #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)
    $lbBackend = Get-AzLoadBalancerBackendAddressPoolConfig -name ($backendItem.Split(",")[0]) -LoadBalancer $newlb
    #write-host "nic"
    $nicRG = $"/")[4]
    $nicToAssociate = Get-AzNetworkInterface -name (($backendItem.Split(",")[1]).split("/")[8]) -resourcegroupname $nicRG
    $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)
    ##look at floating ip setting -enablefloatingIP and loaddistribution
    $floatingIPTest = $newLbRuleConfig.EnableFloatingIP
    $loadDistribution = $newLbRuleConfig.LoadDistribution
    $backendPool = (Get-AzLoadBalancerBackendAddressPoolConfig -LoadBalancer $newlb -Name ((($"/"))[10]))
    $lbFrontEndName = (($newLbRuleConfig.FrontendIPConfiguration).id).Split("/")[10]
    $lbFrontEndNameConfig = ((Get-Variable -Include frontEndIpConfig* | Where-Object {$ -eq $lbFrontEndName})).value
    if ($floatingIPTest.equals($true))
        $newlb | Add-AzLoadBalancerRuleConfig -Name ($newLbRuleConfig).Name -FrontendIPConfiguration $lbFrontEndNameConfig -BackendAddressPool $backendPool -Probe (Get-AzLoadBalancerProbeConfig -LoadBalancer $newlb -Name (($"/")[10])) -Protocol ($newLbRuleConfig).protocol -FrontendPort ($newLbRuleConfig).FrontendPort -BackendPort ($newLbRuleConfig).BackendPort -IdleTimeoutInMinutes ($newLbRuleConfig).IdleTimeoutInMinutes -EnableFloatingIP -LoadDistribution $loadDistribution -DisableOutboundSNAT
        $newlb | Add-AzLoadBalancerRuleConfig -Name ($newLbRuleConfig).Name -FrontendIPConfiguration $lbFrontEndNameConfig -BackendAddressPool $backendPool -Probe (Get-AzLoadBalancerProbeConfig -LoadBalancer $newlb -Name (($"/")[10])) -Protocol ($newLbRuleConfig).protocol -FrontendPort ($newLbRuleConfig).FrontendPort -BackendPort ($newLbRuleConfig).BackendPort -IdleTimeoutInMinutes ($newLbRuleConfig).IdleTimeoutInMinutes -LoadDistribution $loadDistribution -DisableOutboundSNAT
    $newlb | set-AzLoadBalancer

    foreach ($backendIpConfig in $backendPool.BackendIpConfigurations)
        $nicToAssociate = Get-AzNetworkInterface -name (($"/")[8]) -ResourceGroupName $nicRG
        $nicToAssociate.IpConfigurations[0].LoadBalancerBackendAddressPools = $lbBackend
        Set-AzNetworkInterface -NetworkInterface $nicToAssociate