1+ param (
2+ [string ]$Location ,
3+ [string ]$Model ,
4+ [string ]$DeploymentType = " Standard" ,
5+ [int ]$Capacity
6+ )
7+
8+ # Validate parameters
9+ $MissingParams = @ ()
10+ if (-not $Location ) { $MissingParams += " location" }
11+ if (-not $Model ) { $MissingParams += " model" }
12+ if (-not $Capacity ) { $MissingParams += " capacity" }
13+ if (-not $DeploymentType ) { $MissingParams += " deployment-type" }
14+
15+ if ($MissingParams.Count -gt 0 ) {
16+ Write-Error " ❌ ERROR: Missing required parameters: $ ( $MissingParams -join ' , ' ) "
17+ Write-Host " Usage: .\validate_model_quota.ps1 -Location <LOCATION> -Model <MODEL> -Capacity <CAPACITY> [-DeploymentType <DEPLOYMENT_TYPE>]"
18+ exit 1
19+ }
20+
21+ if ($DeploymentType -ne " Standard" -and $DeploymentType -ne " GlobalStandard" ) {
22+ Write-Error " ❌ ERROR: Invalid deployment type: $DeploymentType . Allowed values are 'Standard' or 'GlobalStandard'."
23+ exit 1
24+ }
25+
26+ $ModelType = " OpenAI.$DeploymentType .$Model "
27+ $PreferredRegions = @ (' australiaeast' , ' eastus' , ' eastus2' , ' francecentral' , ' japaneast' , ' norwayeast' , ' southindia' , ' swedencentral' , ' uksouth' , ' westus' , ' westus3' )
28+ $AllResults = @ ()
29+
30+ function Check-Quota {
31+ param (
32+ [string ]$Region
33+ )
34+
35+ try {
36+ $ModelInfoRaw = az cognitiveservices usage list -- location $Region -- query " [?name.value=='$ModelType ']" -- output json
37+ $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+ }
54+ } catch {
55+ return
56+ }
57+ }
58+
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..."
70+ }
71+ } else {
72+ Write-Host " `n ⚠️ Could not retrieve quota info for region '$Location '. Checking fallback regions..."
73+ }
74+
75+ # Remove primary region from fallback list
76+ $FallbackRegions = $PreferredRegions | Where-Object { $_ -ne $Location }
77+
78+ foreach ($region in $FallbackRegions ) {
79+ $result = Check- Quota - Region $region
80+ if ($result ) {
81+ $AllResults += $result
82+ }
83+ }
84+
85+ # Display Results Table
86+ Write-Host " `n -------------------------------------------------------------------------------------------------------------"
87+ Write-Host " | No. | Region | Model Name | Limit | Used | Available |"
88+ Write-Host " -------------------------------------------------------------------------------------------------------------"
89+
90+ $count = 1
91+ foreach ($entry in $AllResults ) {
92+ $modelShort = $entry.Model.Substring ($entry.Model.LastIndexOf (" ." ) + 1 )
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 )
94+ $count ++
95+ }
96+ Write-Host " -------------------------------------------------------------------------------------------------------------"
97+
98+ # Suggest fallback regions
99+ $EligibleFallbacks = $AllResults | Where-Object { $_.Region -ne $Location -and $_.Available -ge $Capacity }
100+
101+ if ($EligibleFallbacks.Count -gt 0 ) {
102+ Write-Host " `n ❌ Deployment cannot proceed in '$Location '."
103+ Write-Host " ➡️ You can retry using one of the following regions with sufficient quota:`n "
104+ foreach ($region in $EligibleFallbacks ) {
105+ Write-Host " • $ ( $region.Region ) (Available: $ ( $region.Available ) )"
106+ }
107+
108+ Write-Host " `n 🔧 To proceed, run:"
109+ Write-Host " azd env set AZURE_AISERVICE_LOCATION '<region>'"
110+ Write-Host " 📌 To confirm it's set correctly, run:"
111+ Write-Host " azd env get-value AZURE_AISERVICE_LOCATION"
112+ Write-Host " ▶️ Once confirmed, re-run azd up to deploy the model in the new region."
113+ exit 2
114+ }
115+
116+ Write-Error " `n ❌ ERROR: No available quota found in any region."
117+ exit 1
0 commit comments