55 [int ]$Capacity
66)
77
8- # Verify required parameters
8+ # Validate parameters
99$MissingParams = @ ()
1010if (-not $Location ) { $MissingParams += " location" }
1111if (-not $Model ) { $MissingParams += " model" }
@@ -24,7 +24,6 @@ if ($DeploymentType -ne "Standard" -and $DeploymentType -ne "GlobalStandard") {
2424}
2525
2626$ModelType = " OpenAI.$DeploymentType .$Model "
27-
2827$PreferredRegions = @ (' australiaeast' , ' eastus' , ' eastus2' , ' francecentral' , ' japaneast' , ' norwayeast' , ' southindia' , ' swedencentral' , ' uksouth' , ' westus' , ' westus3' )
2928$AllResults = @ ()
3029
@@ -33,66 +32,76 @@ function Check-Quota {
3332 [string ]$Region
3433 )
3534
36- $ModelInfoRaw = az cognitiveservices usage list -- location $Region -- query " [?name.value=='$ModelType ']" -- output json
37- $ModelInfo = $null
38-
3935 try {
36+ $ModelInfoRaw = az cognitiveservices usage list -- location $Region -- query " [?name.value=='$ModelType ']" -- output json
4037 $ModelInfo = $ModelInfoRaw | ConvertFrom-Json
38+ if (-not $ModelInfo ) { return }
39+
40+ $CurrentValue = ($ModelInfo | Where-Object { $_.name.value -eq $ModelType }).currentValue
41+ $Limit = ($ModelInfo | Where-Object { $_.name.value -eq $ModelType }).limit
42+
43+ $CurrentValue = [int ]($CurrentValue -replace ' \.0+$' , ' ' )
44+ $Limit = [int ]($Limit -replace ' \.0+$' , ' ' )
45+ $Available = $Limit - $CurrentValue
46+
47+ return [PSCustomObject ]@ {
48+ Region = $Region
49+ Model = $ModelType
50+ Limit = $Limit
51+ Used = $CurrentValue
52+ Available = $Available
53+ }
4154 } catch {
4255 return
4356 }
57+ }
4458
45- if (-not $ModelInfo ) {
46- return
59+ # First, check the user-specified region
60+ Write-Host " `n 🔍 Checking quota in the requested region '$Location '..."
61+ $PrimaryResult = Check- Quota - Region $Location
62+
63+ if ($PrimaryResult ) {
64+ $AllResults += $PrimaryResult
65+ if ($PrimaryResult.Available -ge $Capacity ) {
66+ Write-Host " `n ✅ Sufficient quota found in original region '$Location '."
67+ exit 0
68+ } else {
69+ Write-Host " `n ⚠️ Insufficient quota in '$Location ' (Available: $ ( $PrimaryResult.Available ) , Required: $Capacity ). Checking fallback regions..."
4770 }
71+ } else {
72+ Write-Host " `n ⚠️ Could not retrieve quota info for region '$Location '. Checking fallback regions..."
73+ }
4874
49- $CurrentValue = ($ModelInfo | Where-Object { $_.name.value -eq $ModelType }).currentValue
50- $Limit = ($ModelInfo | Where-Object { $_.name.value -eq $ModelType }).limit
51-
52- $CurrentValue = [int ]($CurrentValue -replace ' \.0+$' , ' ' )
53- $Limit = [int ]($Limit -replace ' \.0+$' , ' ' )
54- $Available = $Limit - $CurrentValue
75+ # Remove primary region from fallback list
76+ $FallbackRegions = $PreferredRegions | Where-Object { $_ -ne $Location }
5577
56- $script :AllResults += [PSCustomObject ]@ {
57- Region = $Region
58- Model = $ModelType
59- Limit = $Limit
60- Used = $CurrentValue
61- Available = $Available
78+ foreach ($region in $FallbackRegions ) {
79+ $result = Check- Quota - Region $region
80+ if ($result ) {
81+ $AllResults += $result
6282 }
6383}
6484
65- foreach ($region in $PreferredRegions ) {
66- Check- Quota - Region $region
67- }
68-
6985# Display Results Table
70- Write-Host " \ n-------------------------------------------------------------------------------------------------------------"
86+ Write-Host " ` n -------------------------------------------------------------------------------------------------------------"
7187Write-Host " | No. | Region | Model Name | Limit | Used | Available |"
7288Write-Host " -------------------------------------------------------------------------------------------------------------"
7389
7490$count = 1
7591foreach ($entry in $AllResults ) {
76- $index = $PreferredRegions.IndexOf ($entry.Region ) + 1
7792 $modelShort = $entry.Model.Substring ($entry.Model.LastIndexOf (" ." ) + 1 )
78- Write-Host (" | {0,-4} | {1,-16} | {2,-35} | {3,-7} | {4,-7} | {5,-9} |" -f $index , $entry.Region , $entry.Model , $entry.Limit , $entry.Used , $entry.Available )
93+ Write-Host (" | {0,-4} | {1,-16} | {2,-35} | {3,-7} | {4,-7} | {5,-9} |" -f $count , $entry.Region , $entry.Model , $entry.Limit , $entry.Used , $entry.Available )
7994 $count ++
8095}
8196Write-Host " -------------------------------------------------------------------------------------------------------------"
8297
83- $EligibleRegion = $AllResults | Where-Object { $_.Region -eq $Location -and $_.Available -ge $Capacity }
84- if ($EligibleRegion ) {
85- Write-Host " \n✅ Sufficient quota found in original region '$Location '."
86- exit 0
87- }
98+ # Suggest fallback regions
99+ $EligibleFallbacks = $AllResults | Where-Object { $_.Region -ne $Location -and $_.Available -ge $Capacity }
88100
89- $FallbackRegions = $AllResults | Where-Object { $_.Region -ne $Location -and $_.Available -ge $Capacity }
90-
91- if ($FallbackRegions.Count -gt 0 ) {
92- Write-Host " `n ❌ Deployment cannot proceed because the original region '$Location ' lacks sufficient quota."
101+ if ($EligibleFallbacks.Count -gt 0 ) {
102+ Write-Host " `n ❌ Deployment cannot proceed in '$Location '."
93103 Write-Host " ➡️ You can retry using one of the following regions with sufficient quota:`n "
94-
95- foreach ($region in $FallbackRegions ) {
104+ foreach ($region in $EligibleFallbacks ) {
96105 Write-Host " • $ ( $region.Region ) (Available: $ ( $region.Available ) )"
97106 }
98107
@@ -104,5 +113,5 @@ if ($FallbackRegions.Count -gt 0) {
104113 exit 2
105114}
106115
107- Write-Error " ❌ ERROR: No available quota found in any region."
116+ Write-Error " `n ❌ ERROR: No available quota found in any region."
108117exit 1
0 commit comments