Find paired Azure region locations with Azure PowerShell

In this article, you learn how to add a custom property to the Azure PowerShell Get-AzLocation cmdlet output to display the physical location for paired regions in Azure. This information isn't available by default but can help you understand the resiliency and redundancy of your Azure resources like geo-redundant storage (GRS) and other Azure services that rely on Azure Storage for replication.

Trying to determine the physical location of paired Azure regions.

The Get-AzLocation cmdlet

The Get-AzLocation cmdlet in Azure PowerShell retrieves a list of all Azure regions and their physical locations. However, the output doesn't include the physical location of paired regions. To get this information, you can use PowerShell to add a custom property that filters and manipulates the output to display the physical location of paired regions as shown in the following example.

1(Get-AzLocation -OutVariable locations) |
2Where-Object GeographyGroup -match 'US' |
3Select-Object -Property Location, PhysicalLocation,
4                        @{Name='PairedRegion';Expression={$_.PairedRegion.Name}},
5                        @{Name='PairedRegionPhysicalLocation';Expression={(
6                            $locations |
7                            Where-Object location -eq $_.PairedRegion.Name).PhysicalLocation}
8                        }

One-liner walkthrough

Although the code is on multiple physical lines, it's still a PowerShell one-liner since it's one continuous pipeline. It might look complex at first, but it can be broken down into several parts for better understanding:

  1. Retrieve the Azure regions:

    1(Get-AzLocation -OutVariable locations)
    

    The Get-AzLocation cmdlet retrieves a list of all Azure locations. The OutVariable parameter stores the output in the locations variable for later use. Wrapping this part of the command in parentheses () ensures it completes before passing any results onto the next command in the pipeline. During my prototyping, I found it necessary to ensure the locations variable was fully populated before the next command in the pipeline was run; otherwise, the locations variable wouldn't have the physical location for all regions. Kudos to Sean Wheeler for helping troubleshoot this issue.

  2. Filter to US regions:

    1Where-Object GeographyGroup -match 'US'
    

    Unfortunately, Get-AzLocation has no parameters for filtering the results, so I used Where-Object. This part of the one-liner filters the locations to include only the regions within the US geography group.

  3. Select relevant properties:

    1Select-Object -Property Location, PhysicalLocation,
    2                        @{Name='PairedRegion';Expression={$_.PairedRegion.Name}},
    3                        @{Name='PairedRegionPhysicalLocation';Expression={(
    4                            $locations |
    5                            Where-Object location -eq $_.PairedRegion.Name).PhysicalLocation}
    6                        }
    

    The Select-Object cmdlet is used to specify what properties to include in the output:

    • Location: The name of the Azure location.
    • PhysicalLocation: The physical location of the Azure datacenter.
    • @{Name='PairedRegion';Expression={$_.PairedRegion.Name}}: A custom property that extracts the name of the paired region.
    • @{Name='PairedRegionPhysicalLocation';Expression={( $locations | Where-Object location -eq $_.PairedRegion.Name).PhysicalLocation}}: A custom property that finds and includes the physical location of the paired region. This is achieved using the information in the locations variable to match the paired region's name and retrieve its physical location.

Explanation of custom properties

PairedRegion

The custom property PairedRegion is created using a calculated expression. The Expression script block {$_.PairedRegion.Name} accesses the PairedRegion property of each location and retrieves its Name.

1@{Name='PairedRegion';Expression={$_.PairedRegion.Name}}

PairedRegionPhysicalLocation

This custom property is slightly more complex. It involves a nested Where-Object cmdlet to find the physical location of the paired region. The script block uses the locations variable to find the location that matches the PairedRegion.Name and then extracts its PhysicalLocation.

1@{Name='PairedRegionPhysicalLocation';Expression={(
2    $locations |
3    Where-Object location -eq $_.PairedRegion.Name).PhysicalLocation}
4}

Running the one-liner

To run this one-liner, you must have the Az PowerShell module installed and be authenticated to your Azure account:

  1. Install the Az PowerShell module (if not already installed):

    1Install-Module -Name Az
    
  2. Connect to your Azure account:

    1Connect-AzAccount
    
  3. Execute the one-liner:

    1(Get-AzLocation -OutVariable locations) |
    2Where-Object GeographyGroup -match 'US' |
    3Select-Object -Property Location, PhysicalLocation,
    4                        @{Name='PairedRegion';Expression={$_.PairedRegion.Name}},
    5                        @{Name='PairedRegionPhysicalLocation';Expression={(
    6                            $locations |
    7                            Where-Object location -eq $_.PairedRegion.Name).PhysicalLocation}
    8                        }
    
     1Location       PhysicalLocation PairedRegion   PairedRegionPhysicalLocation
     2--------       ---------------- ------------   ----------------------------
     3eastus         Virginia         westus         California
     4southcentralus Texas            northcentralus Illinois
     5westus2        Washington       westcentralus  Wyoming
     6westus3        Phoenix          eastus         Virginia
     7centralus      Iowa             eastus2        Virginia
     8eastus2        Virginia         centralus      Iowa
     9northcentralus Illinois         southcentralus Texas
    10westus         California       eastus         Virginia
    11westcentralus  Wyoming          westus2        Washington
    

Summary

The PowerShell one-liner shown in this article demonstrates how you can filter and manipulate the output of Azure PowerShell cmdlets to add custom properties. By understanding each component of the one-liner, you can customize and extend it to meet your specific requirements.

Whether you're an Azure administrator or a DevOps engineer, becoming proficient with Azure PowerShell and these concepts can simplify your cloud management automation while improving your efficiency and productivity.

References