Search code examples
azurepowershellazure-application-gateway

Adding new VM IP Address to Azure Gateway Backend Pool failing


I'm trying to automate the inclusion of an IP Address of a new VM I am creating with an ARM template into an Azure Application Gateway Backend pool.

From what I can gather, I need to get the current pool members and then append the new IP Address to this list and execute the Set-AzApplicationGatewayBackendAddressPool command, followed by the Set-AzApplicationGateway command.

My efforts to date have given me the following errors, regardless of whether I quote my IP Addresses or not. Note that I have tried another string without any spaces in it, which also produced the same result.

Has anyone else run into this before? If so, how did you resolve it?

    $AppGw = Get-AzApplicationGateway -Name "app-gw-name" -ResourceGroupName "Region-Name"
    $BackendPool = Get-AzApplicationGatewayBackendAddressPool -Name "pool-name" -ApplicationGateway $AppGw
    $BackendPoolAddresses = $BackendPool.BackendAddressesText | 
    Select-String -Pattern '"IpAddress":\s*"?([^",]+)"?,?' -allmatches | 
        Foreach-Object { $_.Matches |
            Foreach-Object { $_.Groups[1].Value }
                        }
    $BackendPoolString = '"' + [String]::Join('", "',$BackendPoolAddresses) + '"' + ", " + '"10.0.0.10"'
    $AppGw = Set-AzApplicationGatewayBackendAddressPool -ApplicationGateway $AppGw -Name "pool-name" -BackendIPAddresses $BackendPoolString
    Set-AzApplicationGateway -ApplicationGateway $AppGw
    Set-AzApplicationGateway : Backend Address "10.0.0.6", "10.0.0.7", "10.0.0.8", "10.0.0.9", "10.0.0.10" present within Backend Address Pool pool-name does not meet the format requirements. The address must contain only digits, 
    letters, hyphens and dots for separation. Moreover, it cannot have hyphens at the start, end or next to a dot.
    At line:10 char:1
    + Set-AzApplicationGateway -ApplicationGateway $AppGw
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : CloseError: (:) [Set-AzApplicationGateway], CloudException
        + FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.SetAzureApplicationGatewayCommand
    AppGw = Get-AzApplicationGateway -Name "pp-gw-name" -ResourceGroupName "Region-Name"
    $BackendPool = Get-AzApplicationGatewayBackendAddressPool -Name "pool-name" -ApplicationGateway $AppGw
    $BackendPoolAddresses = $BackendPool.BackendAddressesText | 
    Select-String -Pattern '"IpAddress":\s*"?([^",]+)"?,?' -allmatches | 
        Foreach-Object { $_.Matches |
            Foreach-Object { $_.Groups[1].Value }
                        }
    $BackendPoolString = [String]::Join(", ",$BackendPoolAddresses) + ", " + "10.0.0.10"
    $AppGw = Set-AzApplicationGatewayBackendAddressPool -ApplicationGateway $AppGw -Name "pool-name" -BackendIPAddresses $BackendPoolString
    Set-AzApplicationGateway -ApplicationGateway $AppGw
  Set-AzApplicationGateway : Backend Address 10.0.0.6, 10.0.0.7, 10.0.0.8, 10.0.0.9, 10.0.0.10 present within Backend Address Pool pool-name does not meet the format requirements. The address must contain only digits, letters, hyphens 
  and dots for separation. Moreover, it cannot have hyphens at the start, end or next to a dot.
  At line:10 char:1
  + Set-AzApplicationGateway -ApplicationGateway $AppGw
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : CloseError: (:) [Set-AzApplicationGateway], CloudException
      + FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.SetAzureApplicationGatewayCommand

Solution

  • I tried the same I got same error like below:

    enter image description here

    To add vm Ip address in backend pool make use of below modify script.

    # Set the variables for the Application Gateway and Backend Pool
    $AppGwName = "<AppGatewayName>"
    $ResourceGroupName = "<RGName>"
    $BackendPoolName = "<poolName>"
    $NewIPAddresses = @("10.0.0.6", "10.0.0.7", "10.0.0.8", "10.0.0.9", "10.0.0.10")
    
    # Get the Application Gateway and Backend Pool
    $AppGw = Get-AzApplicationGateway -Name $AppGwName -ResourceGroupName $ResourceGroupName
    $BackendPool = Get-AzApplicationGatewayBackendAddressPool -Name $BackendPoolName -ApplicationGateway $AppGw
    
    # Set the updated backend addresses for the Backend Pool
    Set-AzApplicationGatewayBackendAddressPool -ApplicationGateway $AppGw -Name $BackendPoolName -BackendIPAddresses $NewIPAddresses
    
    # Update the Application Gateway
    Set-AzApplicationGateway -ApplicationGateway $AppGw
    

    Output:

    enter image description here

    After executing the script, in portal backend pool address added successfully like below:

    enter image description here

    To add single Ip address you can use $NewIPAddresses = "10.0.0.10"