| 437-991-3573 | Data Engineering Services
Resources Tools
Experts in Microsoft SQL Server on Windows, Linux, Containers | Clusters, Always On, FCI | Migrations, Cloud, Performance

Powershell - Get Azure Traffic Manager EndPoints Monitor Status
by BF (Principal Consultant; Architecture; Engineering)



[Parameter (Mandatory=$true,Position=1)] [string] $subscription_id,
[Parameter (Mandatory=$true,Position=2)] [string] $resource_group_name,
[Parameter (Mandatory=$true,Position=3)] [string] $profile_name ,
[Parameter (Mandatory=$true,Position=4)] [string] $LogFilePath,
[Parameter (Mandatory=$true,Position=5)] [string] $adTenant

#Define HTTP Headers, RESTAPI URL, Timeout

$RequestHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$RequestHeader.Add("Authorization", "$authHeader")
$RequestHeader.Add("Content-Type", "application/json")

$LogHeaders = "Date&Time(UTC)`tEndPoint-Name`tEndPoint-Status`tMonitor-Status"

add-content -path $LogFilePath -Value $LogHeaders -Force

$RESTAPI_URL = "$subscription_id/resourceGroups/$resource_group_name/providers/Microsoft.Network/trafficManagerProfiles/$profile_name"+"?api-version=2015-11-01";

#Set Timeout
[int] $TimeOut_Sec = 5

#Get the bearer token

#Load ADAL Assemblies
$adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"
#Set Azure AD Tenant name
#Set well-known client ID for Azure PowerShell
$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
#Set redirect URI for Azure PowerShell
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
#Set Resource URI to Azure Service Management API
$resourceAppIdURI = ""
#Set Authority to Azure AD Tenant
$authority = "$adTenant"
#Create Authentication Context tied to Azure AD Tenant
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
#Acquire token
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
#Create Authorization Header
$authHeader = $authResult.CreateAuthorizationHeader()

#Define function for logging in file
function LogDetails
{ param([string]$LogData)

Add-Content -Path $LogFilePath -Value $LogData -Force


#Run infinite loop for the script

while ($true)
#Create a new PS object to hold the resposne JSON
$RESTResponseJSON = New-Object PSObject;


$RESTResponseJSON = (Invoke-RestMethod -Uri $RESTAPI_URL -Method Get -Headers $RequestHeader -TimeoutSec $TimeOut_Sec );

foreach ($ep in $

LogDetails("$((get-date).ToUniversalTime())" +"`t" + $ + "`t" + $ + "`t" + $ )
Write-Host("$((get-date).ToUniversalTime())" +"`t" + $ + "`t" + $ + "`t" + $ )


LogDetails("$((get-date).ToUniversalTime())" + "`t"+ "Error: `t" + $_.Exception.Message);

#Adding delay of 10 seconds, you can change to x it if you want to check the endpoint status in every x seconds
Start-Sleep -Milliseconds 10000

Image #1: