-2

I'm trying to add a new Traffic Manager endpoint to an existing profile in a powershell script as part of a deployment pipeline. In the Azure portal I'm able to simply add a second endpoint and a new priority is assigned. Since I'm using weighted policy the priority is meaningless for me, so I don't care what it is. I don't want to set it with a random number since that could still fail the job.

The documentation claims that new endpoints should be auto assigned a new value, but this only works if I remove all existing endpoints first then add them all back in. Since the get-AzureRMTrafficManagerEndPoint cmdlet requires the endpoint name (not optional) and I don't know all of the endpoint names I have no way to remove and re-add them.

Am I approaching this wrong? Is it a bug? Am I using the commands wrong?

The error text is "Set-AzureRmTrafficManagerProfile : BadRequest: The endpoint priorities are not set correctly. The priorities must be set on all or none of the endpoints."

This code works:

$TrafficManagerProfile = Get-AzureRmTrafficManagerProfile -Name "TMName" -ResourceGroupName "TMRGName"
$publicIPObj = Get-AzureRmPublicIpAddress -ResourceGroupName "App1RGName"
Add-AzureRmTrafficManagerEndpointConfig -EndpointName "App1" -EndpointStatus Enabled -TargetResourceId $publicIPObj.id -TrafficManagerProfile $TrafficManagerProfile -Type AzureEndpoints -Weight 10
$publicIPObj = Get-AzureRmPublicIpAddress -ResourceGroupName "App2RGName"
Add-AzureRmTrafficManagerEndpointConfig -EndpointName "App2" -EndpointStatus Enabled -TargetResourceId $publicIPObj.id -TrafficManagerProfile $TrafficManagerProfile -Type AzureEndpoints -Weight 10
Set-AzureRmTrafficManagerProfile -TrafficManagerProfile $TrafficManagerProfile

This code fails:

   $TrafficManagerProfile = Get-AzureRmTrafficManagerProfile -Name "TMName" -ResourceGroupName "TMRGName"
    $publicIPObj = Get-AzureRmPublicIpAddress -ResourceGroupName "App1RGName"
    Add-AzureRmTrafficManagerEndpointConfig -EndpointName "App1" -EndpointStatus Enabled -TargetResourceId $publicIPObj.id -TrafficManagerProfile $TrafficManagerProfile -Type AzureEndpoints -Weight 10
    Set-AzureRmTrafficManagerProfile -TrafficManagerProfile $TrafficManagerProfile
    $TrafficManagerProfile = Get-AzureRmTrafficManagerProfile -Name "TMName" -ResourceGroupName "TMRGName"
    $publicIPObj = Get-AzureRmPublicIpAddress -ResourceGroupName "App2RGName"
    Add-AzureRmTrafficManagerEndpointConfig -EndpointName "App2" -EndpointStatus Enabled -TargetResourceId $publicIPObj.id -TrafficManagerProfile $TrafficManagerProfile -Type AzureEndpoints -Weight 10
    Set-AzureRmTrafficManagerProfile -TrafficManagerProfile $TrafficManagerProfile
Martyn C
  • 1,109
  • 9
  • 18

1 Answers1

0

The way your code is working and the extra line of $TrafficManagerProfile = Get-AzureRmTrafficManagerProfile -Name "TMName" -ResourceGroupName "TMRGName" is causing this to fail.

You are adding configuration to the Traffic Manager profile for one endpoint, then trying to set it. You need to modify your code so it's the same as your first sample, the difference there is your grabbing the Traffic Manager profile, adding the endpoints and then calling Set-AzureRmTrafficManagerProfile once, rather than twice in your second example.

Edit: Adding some code to get the endpoints in the profile.

$Profiles = Get-AzureRmTrafficManagerProfile -Name mctmp -ResourceGroupName TM

foreach ($Profile in $Profiles.Endpoints.Name) {
    Get-AzureRmTrafficManagerEndpoint -Name $Profile -ProfileName $Profiles.RelativeDnsName -ResourceGroupName TM -Type ExternalEndpoints
}
Martyn C
  • 1,109
  • 9
  • 18
  • Thanks for the reply. That's sort of the point though. I can only do that here because it's sample code. In the application I won't know any details of the first endpoint in the profile so can't remove and add it a second time. Think of the second example as two separate scripts running hours or days apart (different deploys of a site). I've just found that there's also an new-trafficmanagerendpoint command so am giving that a whirl, but I still don't see why the above wouldn't work. – David Lusty Nov 17 '16 at 10:39
  • I also can't find the details of the first endpoint because the get command returns only one endpoint and you have to name it to get it back. Very odd command! – David Lusty Nov 17 '16 at 10:41
  • In the second example, where is the exception thrown, does it give you the line of PS that threw the exception? I would say that you need to run them in separate sessions to try ans simulate what you are trying to achieve. So add the first endpoint, then in a new PS Session add the second endpoint. `Get-AzureRmTrafficManagerProfile` returns both endpoints I have configured in my tests. – Martyn C Nov 17 '16 at 10:47
  • I've added some code above showing how to get the endpoints @DavidLusty – Martyn C Nov 17 '16 at 11:00
  • Many thanks Martin. I managed to get it working with the new-azurermtrafficmanagerendpoint command, but the ability to get all endpoints will definitely come in handy! – David Lusty Nov 17 '16 at 11:30
  • Excellent, glad you found it useful :) – Martyn C Nov 17 '16 at 11:31