Last active
May 8, 2018 22:56
-
-
Save jamesbannan/8fabfebc8f9da3d4410d219312cd642b to your computer and use it in GitHub Desktop.
Get all Azure regions in the same country based on IP address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get all Azure regions accessible with my subscription | |
$regions = az account list-locations | ConvertFrom-Json | |
$locations = $regions | Select-Object displayName,latitude,longitude | Sort-Object displayName | |
# Get my location based on IP address | |
$request = (Invoke-WebRequest -Uri http://ip-api.com/json).Content | ConvertFrom-Json | |
$latitude = $request.lat | |
$longitude = $request.lon | |
$countryCode = $request.countryCode | |
$hash = [ordered]@{ | |
latitude="$latitude"; | |
longitude="$longitude"; | |
countryCode="$countryCode"; | |
locations=@($locations) | |
} | |
$body = $hash | ConvertTo-Json -Depth 100 | |
# Get Azure regions in same country | |
$uri = 'https://azureregion.azurewebsites.net/api/geoPoliticalRegions' | |
$regions = Invoke-RestMethod -Method Put -Uri $uri -Body $body | |
$regions | |
# Get nearest Azure region if no regions in same country | |
if($regions -eq $null){ | |
$uri = 'https://azureregion.azurewebsites.net/api/nearestRegionFromIp' | |
Invoke-RestMethod -Method Put -Uri $uri -Body $body | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script requires PowerShell (not Azure PowerShell) and Azure CLI