Skip to content

Instantly share code, notes, and snippets.

@jamesbannan
Created May 8, 2018 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesbannan/73ffa90133d2bc66941952f52cfeb1e3 to your computer and use it in GitHub Desktop.
Save jamesbannan/73ffa90133d2bc66941952f52cfeb1e3 to your computer and use it in GitHub Desktop.
Find nearest Azure region based on IP address
# 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
$hash = [ordered]@{
latitude="$latitude";
longitude="$longitude";
locations=@($locations)
}
$body = $hash | ConvertTo-Json -Depth 100
# Get nearest Azure region
$uri = 'https://azureregion.azurewebsites.net/api/nearestRegionFromIp'
Invoke-RestMethod -Method Put -Uri $uri -Body $body
@jamesbannan
Copy link
Author

To run this script you need PowerShell (not Azure PowerShell) and the Azure CLI.

@Bill7zz
Copy link

Bill7zz commented May 24, 2019

Hi, I got this error message:
The remote name could not be resolved: 'azureregion.azurewebsites.net'
I'm running the script locally using PowerShell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment