-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGet-AzAvailabilityZone.ps1
62 lines (52 loc) · 2.28 KB
/
Get-AzAvailabilityZone.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Connect to Azure using Get-AzAccount
Connect-AzAccount
# Set the region to 'Australia East'
$region = 'Australia East'
# Get all subscriptions that the account has access to
$subscriptions = Get-AzSubscription | Select-Object -ExpandProperty SubscriptionId
# Get the access token for the authenticated user
$token = (Get-AzAccessToken).Token
# Check if AvailabilityZonePeering feature is enabled and enable it if it's not
$azFeature = Get-AzProviderFeature -ProviderNamespace Microsoft.Resources -FeatureName AvailabilityZonePeering
if (!$azFeature.RegistrationState.Equals("Registered")) {
do {
Register-AzProviderFeature -FeatureName AvailabilityZonePeering -ProviderNamespace Microsoft.Resources
Start-Sleep -Seconds 5
$azFeature = Get-AzProviderFeature -ProviderNamespace Microsoft.Resources -FeatureName AvailabilityZonePeering
} until ($azFeature.RegistrationState.Equals("Registered"))
Write-Host "The AvailabilityZonePeering feature has been enabled."
}
else {
Write-Host "The AvailabilityZonePeering feature is already enabled."
}
# Define the request body for the REST API call
$body = @{
subscriptionIds = $subscriptions | ForEach-Object { 'subscriptions/' + $_ }
location = $region
} | ConvertTo-Json
# Define the request parameters for the REST API call
$params = @{
Uri = "https://management.azure.com/subscriptions/" + $subscriptions[1] +
"/providers/Microsoft.Resources/checkZonePeers/?api-version=2020-01-01"
Headers = @{ 'Authorization' = "Bearer $token" }
Method = 'POST'
Body = $body
ContentType = 'application/json'
}
# Invoke the REST API and store the response
$availabilityZonePeers = Invoke-RestMethod @Params
# Initialize an empty array for the output
$output = @()
# Loop through each availability zone and its associated peers and add them to the output array
foreach ($i in $availabilityZonePeers.availabilityZonePeers.availabilityZone) {
foreach ($zone in $availabilityZonePeers.availabilityZonePeers[$i - 1].peers ) {
$output += New-Object PSObject -Property @{
Zone = $i
MatchesZone = $zone.availabilityZone
SubscriptionId = $zone.subscriptionId
}
}
$output += ""
}
# Output the results
$output | Format-Table