-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathpreprovision-integrated.ps1
More file actions
550 lines (468 loc) · 22.2 KB
/
Copy pathpreprovision-integrated.ps1
File metadata and controls
550 lines (468 loc) · 22.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# Custom preprovision script that integrates AI Landing Zone Template Specs
# This script:
# 1. Runs AI Landing Zone's preprovision to create Template Specs
# 2. Uses our parameters (infra/main.bicepparam) with the optimized deployment
param(
[string]$Location = $env:AZURE_LOCATION,
[string]$ResourceGroup = $env:AZURE_RESOURCE_GROUP,
[string]$SubscriptionId = $env:AZURE_SUBSCRIPTION_ID
)
$ErrorActionPreference = 'Stop'
# Ensure $env:TEMP is set (not defined by default on Linux)
if (-not $env:TEMP) { $env:TEMP = '/tmp' }
Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host " AI Landing Zone - Integrated Preprovision" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""
$repoRootResolved = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
function Resolve-AzdEnvironmentValues {
param(
[string]$Location,
[string]$ResourceGroup,
[string]$SubscriptionId
)
$missing = @()
if ([string]::IsNullOrWhiteSpace($Location)) { $missing += 'AZURE_LOCATION' }
if ([string]::IsNullOrWhiteSpace($ResourceGroup)) { $missing += 'AZURE_RESOURCE_GROUP' }
if ([string]::IsNullOrWhiteSpace($SubscriptionId)) { $missing += 'AZURE_SUBSCRIPTION_ID' }
if ($missing.Count -eq 0) {
return @{ Location = $Location; ResourceGroup = $ResourceGroup; SubscriptionId = $SubscriptionId }
}
try {
$azd = Get-Command azd -ErrorAction SilentlyContinue
if ($null -ne $azd) {
$json = & azd env get-values --output json 2>$null
if (-not [string]::IsNullOrWhiteSpace($json)) {
$values = $json | ConvertFrom-Json
if ([string]::IsNullOrWhiteSpace($Location) -and $values.AZURE_LOCATION) { $Location = [string]$values.AZURE_LOCATION }
if ([string]::IsNullOrWhiteSpace($ResourceGroup) -and $values.AZURE_RESOURCE_GROUP) { $ResourceGroup = [string]$values.AZURE_RESOURCE_GROUP }
if ([string]::IsNullOrWhiteSpace($SubscriptionId) -and $values.AZURE_SUBSCRIPTION_ID) { $SubscriptionId = [string]$values.AZURE_SUBSCRIPTION_ID }
}
}
} catch {
# Ignore and fall back to other methods/prompting.
}
if ([string]::IsNullOrWhiteSpace($SubscriptionId)) {
try {
$az = Get-Command az -ErrorAction SilentlyContinue
if ($null -ne $az) {
$sub = (& az account show --query id -o tsv 2>$null)
if (-not [string]::IsNullOrWhiteSpace($sub)) {
$SubscriptionId = $sub.Trim()
}
}
} catch {
# Ignore and fall back to prompting.
}
}
return @{ Location = $Location; ResourceGroup = $ResourceGroup; SubscriptionId = $SubscriptionId }
}
$resolved = Resolve-AzdEnvironmentValues -Location $Location -ResourceGroup $ResourceGroup -SubscriptionId $SubscriptionId
$Location = $resolved.Location
$ResourceGroup = $resolved.ResourceGroup
$SubscriptionId = $resolved.SubscriptionId
if ([string]::IsNullOrWhiteSpace($env:AZURE_LOCATION) -and -not [string]::IsNullOrWhiteSpace($Location)) {
$env:AZURE_LOCATION = $Location
}
if ([string]::IsNullOrWhiteSpace($env:AZURE_COSMOS_LOCATION) -and -not [string]::IsNullOrWhiteSpace($Location)) {
$env:AZURE_COSMOS_LOCATION = $Location
}
if ([string]::IsNullOrWhiteSpace($env:AZURE_PRINCIPAL_ID)) {
try {
$fromAzd = (& azd env get-value AZURE_PRINCIPAL_ID 2>$null).ToString().Trim()
if (-not [string]::IsNullOrWhiteSpace($fromAzd)) {
$env:AZURE_PRINCIPAL_ID = $fromAzd
}
} catch {
# Ignore and fall back to other methods.
}
}
$isGuid = $false
if (-not [string]::IsNullOrWhiteSpace($env:AZURE_PRINCIPAL_ID)) {
$isGuid = $env:AZURE_PRINCIPAL_ID -match '^[0-9a-fA-F-]{36}$'
}
if (-not $isGuid) {
try {
$acctType = (& az account show --query user.type -o tsv 2>$null).Trim()
$acctName = (& az account show --query user.name -o tsv 2>$null).Trim()
if ($acctType -eq 'user') {
$principal = (& az ad signed-in-user show --query id -o tsv 2>$null)
if ([string]::IsNullOrWhiteSpace($principal) -and -not [string]::IsNullOrWhiteSpace($acctName)) {
$principal = (& az ad user show --id $acctName --query id -o tsv 2>$null)
}
} elseif ($acctType -eq 'servicePrincipal') {
if (-not [string]::IsNullOrWhiteSpace($acctName)) {
$principal = (& az ad sp show --id $acctName --query id -o tsv 2>$null)
}
}
if (-not [string]::IsNullOrWhiteSpace($principal) -and ($principal -match '^[0-9a-fA-F-]{36}$')) {
$env:AZURE_PRINCIPAL_ID = $principal.Trim()
$isGuid = $true
}
} catch {
# Ignore and fall back to provided values.
}
}
if ([string]::IsNullOrWhiteSpace($env:AZURE_PRINCIPAL_ID)) {
try {
$acctType = (& az account show --query user.type -o tsv 2>$null).Trim()
$acctName = (& az account show --query user.name -o tsv 2>$null).Trim()
if ($acctType -eq 'user') {
$principal = (& az ad signed-in-user show --query id -o tsv 2>$null)
if ([string]::IsNullOrWhiteSpace($principal) -and -not [string]::IsNullOrWhiteSpace($acctName)) {
$principal = (& az ad user show --id $acctName --query id -o tsv 2>$null)
}
} elseif ($acctType -eq 'servicePrincipal') {
if (-not [string]::IsNullOrWhiteSpace($acctName)) {
$principal = (& az ad sp show --id $acctName --query id -o tsv 2>$null)
}
}
if (-not [string]::IsNullOrWhiteSpace($principal)) {
$env:AZURE_PRINCIPAL_ID = $principal.Trim()
}
} catch {
# Ignore and fall back to provided values.
}
}
if ([string]::IsNullOrWhiteSpace($env:NETWORK_ISOLATION)) {
try {
$ni = (& azd env get-value NETWORK_ISOLATION 2>$null).ToString().Trim()
if (-not [string]::IsNullOrWhiteSpace($ni)) {
$env:NETWORK_ISOLATION = $ni
}
} catch {
# Ignore and fall back to defaults.
}
}
# In non-interactive hook execution (azure.yaml sets interactive:false), Read-Host prompts are not usable.
# If the resource group is missing, derive a deterministic default from AZURE_ENV_NAME.
if ([string]::IsNullOrWhiteSpace($ResourceGroup)) {
$envName = $env:AZURE_ENV_NAME
if ([string]::IsNullOrWhiteSpace($envName)) {
try {
$envName = (& azd env get-value AZURE_ENV_NAME 2>$null).ToString().Trim()
} catch {
$envName = $null
}
}
if (-not [string]::IsNullOrWhiteSpace($envName)) {
$ResourceGroup = "rg-$envName"
try { & azd env set AZURE_RESOURCE_GROUP $ResourceGroup 2>$null | Out-Null } catch { }
Write-Host "[i] AZURE_RESOURCE_GROUP not set; defaulting to '$ResourceGroup'." -ForegroundColor Yellow
}
}
if ([string]::IsNullOrWhiteSpace($Location)) {
$Location = Read-Host "Enter Azure location (AZURE_LOCATION)"
}
if ([string]::IsNullOrWhiteSpace($ResourceGroup)) {
$ResourceGroup = Read-Host "Enter resource group name (AZURE_RESOURCE_GROUP)"
}
if ([string]::IsNullOrWhiteSpace($SubscriptionId)) {
$SubscriptionId = Read-Host "Enter subscription ID (AZURE_SUBSCRIPTION_ID)"
}
if ([string]::IsNullOrWhiteSpace($Location) -or [string]::IsNullOrWhiteSpace($ResourceGroup) -or [string]::IsNullOrWhiteSpace($SubscriptionId)) {
Write-Host "[X] Missing required Azure context (location/resource group/subscription)." -ForegroundColor Red
Write-Host " Tip: run 'azd env select <env>' then re-run, or set AZURE_LOCATION/AZURE_RESOURCE_GROUP/AZURE_SUBSCRIPTION_ID." -ForegroundColor Yellow
exit 1
}
# Navigate to AI Landing Zone submodule
$aiLandingZonePath = Join-Path $PSScriptRoot ".." "submodules" "ai-landing-zone"
if (-not (Test-Path $aiLandingZonePath)) {
Write-Host "[!] AI Landing Zone submodule not initialized" -ForegroundColor Yellow
Write-Host " Initializing submodule automatically..." -ForegroundColor Cyan
# Navigate to repo root
$repoRoot = Join-Path $PSScriptRoot ".."
Push-Location $repoRoot
try {
# Initialize and update submodules
git submodule update --init --recursive
if ($LASTEXITCODE -ne 0) {
Write-Host "[X] Failed to initialize git submodules" -ForegroundColor Red
Write-Host " Try running manually: git submodule update --init --recursive" -ForegroundColor Yellow
exit 1
}
Write-Host " [+] Submodule initialized successfully" -ForegroundColor Green
} finally {
Pop-Location
}
# Verify it now exists
if (-not (Test-Path $aiLandingZonePath)) {
Write-Host "[X] Submodule still not found after initialization!" -ForegroundColor Red
exit 1
}
}
Write-Host "[1] Deploying AI Landing Zone submodule..." -ForegroundColor Cyan
Write-Host ""
$submoduleMain = Join-Path $aiLandingZonePath "main.bicep"
if (-not (Test-Path $submoduleMain)) {
Write-Host "[X] AI Landing Zone main.bicep not found!" -ForegroundColor Red
Write-Host " Expected: $submoduleMain" -ForegroundColor Yellow
exit 1
}
$parentParamsFile = Join-Path $PSScriptRoot ".." "infra" "main.bicepparam"
if (-not (Test-Path $parentParamsFile)) {
Write-Host "[X] Parent parameters file not found!" -ForegroundColor Red
Write-Host " Expected: $parentParamsFile" -ForegroundColor Yellow
exit 1
}
$az = Get-Command az -ErrorAction SilentlyContinue
if ($null -eq $az) {
Write-Host "[X] Azure CLI (az) not found in PATH." -ForegroundColor Red
exit 1
}
Write-Host " [+] Submodule template: $submoduleMain" -ForegroundColor Green
Write-Host " [+] Parent params file: $parentParamsFile" -ForegroundColor Green
if (-not [string]::IsNullOrWhiteSpace($SubscriptionId)) {
& az account set --subscription $SubscriptionId | Out-Null
}
$envNameForDeployment = $env:AZURE_ENV_NAME
if ([string]::IsNullOrWhiteSpace($envNameForDeployment)) { $envNameForDeployment = 'default' }
$deploymentName = "ai-landing-zone-$envNameForDeployment-$(Get-Date -Format 'yyyyMMddHHmmss')"
$deploymentRetryCount = 6
$deploymentRetryDelaySeconds = 30
Write-Host " [+] Deployment name: $deploymentName" -ForegroundColor Green
function Format-AzDeploymentError {
param(
[string]$Raw
)
$code = $null
$message = $null
$rawText = $Raw
if (-not [string]::IsNullOrWhiteSpace($Raw)) {
try {
$json = $Raw | ConvertFrom-Json -ErrorAction Stop
if ($null -ne $json.error) {
$code = $json.error.code
$message = $json.error.message
if ($json.error.details -and $json.error.details.Count -gt 0) {
$detail = $json.error.details[0]
if ($detail.code) { $code = $detail.code }
if ($detail.message) { $message = $detail.message }
}
}
} catch {
# Not JSON, fall back to regex matching below.
}
if (-not $code -and $Raw -match 'DeploymentActive') {
$code = 'DeploymentActive'
}
if (-not $code -and $Raw -match 'AccountProvisioningStateInvalid') {
$code = 'AccountProvisioningStateInvalid'
}
if (-not $code -and $Raw -match "management\.azure\.com") {
$code = 'NetworkResolutionFailed'
}
if ([string]::IsNullOrWhiteSpace($message)) {
$lines = $Raw -split "`r?`n" | Where-Object { $_ -and $_ -notmatch '^WARNING:' }
$message = ($lines | Select-Object -First 3) -join ' '
}
}
return [pscustomobject]@{
Code = $code
Message = $message
Raw = $rawText
}
}
$compiledParent = Join-Path $env:TEMP ("parent.$deploymentName.parameters.json")
& az bicep build-params --file $parentParamsFile --outfile $compiledParent | Out-Null
if ($LASTEXITCODE -ne 0 -or -not (Test-Path $compiledParent)) {
Write-Host "[X] Failed to compile parent bicepparam to JSON: $compiledParent" -ForegroundColor Red
exit 1
}
$allowedParamNames = Select-String -Path $submoduleMain -Pattern '^param\s+(\w+)' | ForEach-Object {
$_.Matches[0].Groups[1].Value
} | Sort-Object -Unique
$parentJson = Get-Content $compiledParent -Raw | ConvertFrom-Json
$parentPrincipal = $null
try {
$parentPrincipal = [string]$parentJson.parameters.principalId.value
} catch {
$parentPrincipal = $null
}
if ([string]::IsNullOrWhiteSpace($parentPrincipal)) {
Write-Host "[X] principalId is empty in infra/main.bicepparam. Set it to your Entra Object ID (GUID)." -ForegroundColor Red
exit 1
}
$parentPrincipal = $parentPrincipal.Trim()
if ($parentPrincipal -notmatch '^[0-9a-fA-F-]{36}$') {
Write-Host "[X] principalId must be a GUID. Current value: '$parentPrincipal'" -ForegroundColor Red
exit 1
}
$env:AZURE_PRINCIPAL_ID = $parentPrincipal
try {
& azd env set AZURE_PRINCIPAL_ID $env:AZURE_PRINCIPAL_ID 2>$null | Out-Null
} catch {
# Ignore and proceed.
}
$filtered = [ordered]@{
'$schema' = $parentJson.'$schema'
contentVersion = $parentJson.contentVersion
parameters = @{}
}
foreach ($name in $allowedParamNames) {
$value = $parentJson.parameters.$name
if ($null -ne $value) {
$filtered.parameters[$name] = $value
}
}
$filteredParams = Join-Path $env:TEMP ("ai-landing-zone.$deploymentName.parameters.json")
$filtered | ConvertTo-Json -Depth 50 | Set-Content -Path $filteredParams -Encoding UTF8
$deployOutput = $null
$deployExitCode = 1
$parsed = $null
$raw = $null
for ($attempt = 1; $attempt -le $deploymentRetryCount; $attempt++) {
$deployOutput = & az deployment group create --name $deploymentName --resource-group $ResourceGroup --template-file $submoduleMain --parameters ("@" + $filteredParams) --only-show-errors 2>&1
$deployExitCode = $LASTEXITCODE
if ($deployExitCode -eq 0) {
break
}
$raw = ($deployOutput | Out-String).Trim()
$parsed = Format-AzDeploymentError -Raw $raw
if ($parsed.Code -ne 'AccountProvisioningStateInvalid' -or $attempt -eq $deploymentRetryCount) {
break
}
Write-Host " [!] AI Foundry account is still provisioning (attempt $attempt/$deploymentRetryCount). Waiting ${deploymentRetryDelaySeconds}s before retry..." -ForegroundColor Yellow
Start-Sleep -Seconds $deploymentRetryDelaySeconds
}
if ($deployExitCode -ne 0) {
Write-Host "[X] AI Landing Zone submodule deployment failed" -ForegroundColor Red
if (-not $raw) {
$raw = ($deployOutput | Out-String).Trim()
}
if (-not $parsed) {
$parsed = Format-AzDeploymentError -Raw $raw
}
if (-not [string]::IsNullOrWhiteSpace($parsed.Code) -or -not [string]::IsNullOrWhiteSpace($parsed.Message)) {
$reasonParts = @()
if ($parsed.Code) { $reasonParts += $parsed.Code }
if ($parsed.Message) { $reasonParts += $parsed.Message }
Write-Host (" Failure: {0}" -f ($reasonParts -join " - ")) -ForegroundColor Yellow
}
if ($parsed.Code -eq 'DeploymentActive') {
Write-Host " Another deployment is still running in this resource group. Wait for it to complete or cancel it, then re-run." -ForegroundColor Yellow
} elseif ($parsed.Code -eq 'AccountProvisioningStateInvalid') {
Write-Host " AI Foundry account is still provisioning. Retry after it reaches 'Succeeded'." -ForegroundColor Yellow
} elseif ($parsed.Code -eq 'NetworkResolutionFailed') {
Write-Host " Network/DNS could not resolve management.azure.com. Check connectivity and retry." -ForegroundColor Yellow
}
if ($env:AZD_VERBOSE_ERRORS -and -not [string]::IsNullOrWhiteSpace($raw)) {
$preview = ($raw -split "`r?`n" | Select-Object -First 5) -join "`n"
Write-Host " Raw error (first lines):" -ForegroundColor DarkGray
Write-Host $preview -ForegroundColor DarkGray
}
exit 1
}
Write-Host " [+] AI Landing Zone deployment complete" -ForegroundColor Green
Write-Host ""
Write-Host "[2] Publishing submodule outputs to azd env..." -ForegroundColor Cyan
function Set-AzdEnvValue {
param(
[string]$Name,
[string]$Value
)
if ([string]::IsNullOrWhiteSpace($Value)) { return }
try {
& azd env set $Name $Value 2>$null | Out-Null
} catch {
# Ignore and continue.
}
}
$aiSearchName = $null
try { $aiSearchName = [string]$parentJson.parameters.searchServiceName.value } catch { }
if ([string]::IsNullOrWhiteSpace($aiSearchName)) {
try { $aiSearchName = [string]$parentJson.parameters.aiFoundrySearchServiceName.value } catch { }
}
if ([string]::IsNullOrWhiteSpace($aiSearchName)) {
try { $aiSearchName = (az search service list --resource-group $ResourceGroup --query "[0].name" -o tsv 2>$null).Trim() } catch { }
}
# BYO existing AI Foundry project: when AZURE_EXISTING_AI_PROJECT_RESOURCE_ID is
# set (and surfaced through the bicepparam as existingAiProjectResourceId), parse
# its segments and use them for the downstream azd env values so postprovision
# automation targets the existing account/project instead of attempting RG
# discovery in this deployment's resource group.
$existingAiProjectResourceId = $null
try { $existingAiProjectResourceId = [string]$parentJson.parameters.existingAiProjectResourceId.value } catch { }
if ([string]::IsNullOrWhiteSpace($existingAiProjectResourceId)) {
$existingAiProjectResourceId = $env:AZURE_EXISTING_AI_PROJECT_RESOURCE_ID
}
$aiFoundryName = $null
$aiFoundryProjectName = $null
$aiFoundryResourceGroupForEnv = $ResourceGroup
$aiFoundrySubscriptionForEnv = $SubscriptionId
if (-not [string]::IsNullOrWhiteSpace($existingAiProjectResourceId)) {
Write-Host " [i] BYO AI Foundry project detected: $existingAiProjectResourceId" -ForegroundColor Cyan
$segments = $existingAiProjectResourceId.Trim('/').Split('/')
# Expected: subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.CognitiveServices/accounts/{account}/projects/{project}
if ($segments.Length -ge 10 -and $segments[0] -ieq 'subscriptions' -and $segments[2] -ieq 'resourceGroups' -and $segments[6] -ieq 'accounts' -and $segments[8] -ieq 'projects') {
$aiFoundrySubscriptionForEnv = $segments[1]
$aiFoundryResourceGroupForEnv = $segments[3]
$aiFoundryName = $segments[7]
$aiFoundryProjectName = $segments[9]
Write-Host " Account: $aiFoundryName" -ForegroundColor Green
Write-Host " Project: $aiFoundryProjectName" -ForegroundColor Green
Write-Host " ResourceGrp: $aiFoundryResourceGroupForEnv" -ForegroundColor Green
Write-Host " Subscription: $aiFoundrySubscriptionForEnv" -ForegroundColor Green
} else {
Write-Host " [!] AZURE_EXISTING_AI_PROJECT_RESOURCE_ID is set but the resource ID format is not recognized. Falling back to discovery." -ForegroundColor Yellow
$existingAiProjectResourceId = $null
}
}
if ([string]::IsNullOrWhiteSpace($aiFoundryName)) {
try { $aiFoundryName = [string]$parentJson.parameters.aiFoundryAccountName.value } catch { }
if ([string]::IsNullOrWhiteSpace($aiFoundryName)) {
try {
$aiFoundryName = (az cognitiveservices account list --resource-group $ResourceGroup --query "[?kind=='AIServices']|[0].name" -o tsv 2>$null).Trim()
} catch { }
}
}
if ([string]::IsNullOrWhiteSpace($aiFoundryProjectName)) {
try { $aiFoundryProjectName = [string]$parentJson.parameters.aiFoundryProjectName.value } catch { }
if ([string]::IsNullOrWhiteSpace($aiFoundryProjectName) -and -not [string]::IsNullOrWhiteSpace($aiFoundryName)) {
try {
$projectCandidatesRaw = az resource list --resource-group $aiFoundryResourceGroupForEnv --subscription $aiFoundrySubscriptionForEnv --resource-type "Microsoft.CognitiveServices/accounts/projects" --query "[?contains(id, '/accounts/$aiFoundryName/')].name" -o tsv 2>$null
if ($projectCandidatesRaw) {
[string[]]$projectCandidates = ($projectCandidatesRaw -split "\r?\n") | Where-Object { $_ -and $_.Trim() } | ForEach-Object { $_.Trim() }
if ($projectCandidates.Length -ge 1) {
$aiFoundryProjectName = $projectCandidates[0]
}
}
} catch {
# Ignore discovery failures and continue.
}
}
}
$aiSearchResourceId = $null
if (-not [string]::IsNullOrWhiteSpace($aiSearchName)) {
try { $aiSearchResourceId = (az resource show --resource-group $ResourceGroup --name $aiSearchName --resource-type Microsoft.Search/searchServices --query id -o tsv 2>$null).Trim() } catch { }
}
Set-AzdEnvValue -Name 'aiSearchName' -Value $aiSearchName
Set-AzdEnvValue -Name 'AZURE_AI_SEARCH_NAME' -Value $aiSearchName
Set-AzdEnvValue -Name 'aiSearchResourceId' -Value $aiSearchResourceId
Set-AzdEnvValue -Name 'aiSearchResourceGroup' -Value $ResourceGroup
Set-AzdEnvValue -Name 'aiSearchSubscriptionId' -Value $SubscriptionId
Set-AzdEnvValue -Name 'aiFoundryName' -Value $aiFoundryName
Set-AzdEnvValue -Name 'aiFoundryResourceGroup' -Value $aiFoundryResourceGroupForEnv
Set-AzdEnvValue -Name 'aiFoundrySubscriptionId' -Value $aiFoundrySubscriptionForEnv
Set-AzdEnvValue -Name 'aiFoundryProjectName' -Value $aiFoundryProjectName
if (-not [string]::IsNullOrWhiteSpace($existingAiProjectResourceId)) {
Set-AzdEnvValue -Name 'AZURE_EXISTING_AI_PROJECT_RESOURCE_ID' -Value $existingAiProjectResourceId
Set-AzdEnvValue -Name 'existingAiProjectResourceId' -Value $existingAiProjectResourceId
Set-AzdEnvValue -Name 'useExistingAiProject' -Value 'true'
}
Write-Host ""
Write-Host "[OK] Preprovision complete!" -ForegroundColor Green
try {
Write-PreprovisionMarker -RepoRoot $repoRootResolved -Location $Location -ResourceGroup $ResourceGroup -SubscriptionId $SubscriptionId
} catch {
# Best-effort marker. Ignore failures so we don't block provisioning.
}
Write-Host ""
Write-Host " Template Specs created in resource group: $ResourceGroup" -ForegroundColor White
Write-Host " Deploy directory with Template Spec references ready" -ForegroundColor White
Write-Host " Your parameters (infra/main.bicepparam) will be used for deployment" -ForegroundColor White
Write-Host ""
Write-Host " Next: azd will provision using optimized Template Specs" -ForegroundColor Cyan
Write-Host " (avoids ARM 4MB template size limit)" -ForegroundColor Cyan
Write-Host ""