Search code examples
automationazure-powershellazure-front-door

How to update existing routing rule in Azure Frontdoor using PowerShell?


I need to update the backend pool (Maintenance) used by an existing routing rule in Azure Frontdoor to a different existing backend pool (Maintenance2). Here is the UI screen from where it can be done. Can someone advise on how to do this via PowerShell. I have tried via the cmdlets (https://learn.microsoft.com/en-us/powershell/module/az.frontdoor/set-azfrontdoor?view=azps-9.0.1 ) but unable to get the correct set of commands.

enter image description here

I have tried via the cmdlets (https://learn.microsoft.com/en-us/powershell/module/az.frontdoor/set-azfrontdoor?view=azps-9.0.1 ) but unable to get the correct set of commands.


Solution

  • Thank you Swarna. The solution provided is in CLI and the question was for powershell.

    I was able to figure out how to do this in PowerShell. It requires the use of 3 Azure PS cmdlets- Get-AzFrontDoor, New-AzFrontDoorRoutingRuleObject and Set-AzFrontDoor. The way it works in the background is that when an update is performed on the Routing Rule, the routing rule is deleted and recreated with the changes. In-order to do this via PS, we have to get the existing frontdoor properties, routing rule properties and put the changes in New-AzFrontDoorRoutingRuleObject. Lastly use Set-AzFrontDoor to apply the changes to frontdoor.

    $subscription='Sub1'
    Select-AzSubscription $Sub1
    $frontdoorName='Frontdoor1'
    $resourcegroupname='fdrrg'
    $MaintenanceBackPool='Maintenance2'
    $PrimaryBackPool='Maintenance1'
    $RoutingRuleName='Route1'
    
    #get the current frontdoor property object
    $frontdoorobj=Get-AzFrontDoor -ResourceGroupName $resourcegroupname -Name $frontdoorName
    
    #get the Routing Rules and filter the one which needs to be modified
    $RoutingRuleUpdate=$frontdoorobj.RoutingRules
    $RoutingRuleUpdate2=$RoutingRuleUpdate|Where-Object {$_.Name -contains $RoutingRuleName}
    
    #get the list of all frontendendpointIds as an array (this is required to account for more than 1 frontends/domains associated with the routing rule)
    #Perform string manipulation to get the frontend/domain name from the ID
    [String[]] $frontdoorHostnames=$RoutingRuleUpdate2.FrontendEndpointIds | ForEach-Object {"$PSItem" -replace '.*/'}
    
    #get the position of the Routing Rule (to be modified) in the Routing Rules collection
    $i=[array]::indexof($RoutingRuleUpdate.Name,$RoutingRuleName)
    
    #Update the Routing Rule object with the changes needed- in this case a different backendpool
    $updatedRouteObj=New-AzFrontDoorRoutingRuleObject -Name $RoutingRuleUpdate[$i].Name  -FrontDoorName $frontDoorName -ResourceGroupName $resourcegroupname -FrontendEndpointName $frontdoorHostnames -BackendPoolName $MaintenanceBackPool
    $RoutingRuleUpdate[$i]=$updatedRouteObj
    
    #Finally update the frontdoor object with the change in Routing Rule
    Set-AzFrontDoor -InputObject $frontdoorobj -RoutingRule $RoutingRuleUpdate 
    Write-Output "Successfully Updated RoutingRule:$RoutingRuleName to backendpool:$MaintenanceBackPool"**