Solution:
[CmdletBinding()]
Param( [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
#REST API URL $RESTAPI_URL = "https://management.azure.com/subscriptions/$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 #Credit: https://blogs.technet.microsoft.com/keithmayer/2014/12/30/leveraging-the-azure-service-management-rest-api-with-azure-active-directory-and-powershell-list-azure-administrators/
#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" [System.Reflection.Assembly]::LoadFrom($adal) [System.Reflection.Assembly]::LoadFrom($adalforms) #Set Azure AD Tenant name #$adTenant #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 = "https://management.core.windows.net/" #Set Authority to Azure AD Tenant $authority = "https://login.windows.net/$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; Try { $RESTResponseJSON = (Invoke-RestMethod -Uri $RESTAPI_URL -Method Get -Headers $RequestHeader -TimeoutSec $TimeOut_Sec ); foreach ($ep in $RESTResponseJSON.properties.endpoints)
{ LogDetails("$((get-date).ToUniversalTime())" +"`t" + $ep.name + "`t" + $ep.properties.endpointStatus + "`t" + $ep.properties.endpointMonitorStatus ) Write-Host("$((get-date).ToUniversalTime())" +"`t" + $ep.name + "`t" + $ep.properties.endpointStatus + "`t" + $ep.properties.endpointMonitorStatus ) } } Catch { 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:
|
|
|
|
|