Skip to content

Instantly share code, notes, and snippets.

@jamesbannan
Last active May 8, 2018 22:56
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/8fabfebc8f9da3d4410d219312cd642b to your computer and use it in GitHub Desktop.
Save jamesbannan/8fabfebc8f9da3d4410d219312cd642b to your computer and use it in GitHub Desktop.
Get all Azure regions in the same country 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
$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
}
@jamesbannan
Copy link
Author

This script requires PowerShell (not Azure PowerShell) and Azure CLI

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