Setting the file. One moment.
Validate Deployment · Azure Validate · microsoft/azure-skills · Skills Docs
ContentsBack to the top of the page 28.8
Errors · azd
(opens in a new tab)
references/recipes/scripts/ validate-deployment.ps1
PowerShell · 155 lines · 5 KB
Exit codes: 0 = every step passed; 1 = a validation step failed;
15 2 = usage / argument error.
16 . PARAMETER Scope
17 Deployment scope: 'sub' or 'group' (required).
18 . PARAMETER Location
19 Location (required when -Scope sub).
20 . PARAMETER ResourceGroup
21 Resource group name (required when -Scope group).
22 . PARAMETER Template
23 Bicep template path. Default: ./infra/main.bicep
24 . PARAMETER Parameters
25 Parameters file path. Default: ./infra/main.parameters.json
26 (skipped automatically if the file does not exist).
27 . PARAMETER Subscription
28 Subscription to target (optional).
29 . EXAMPLE
30 .\validate-deployment.ps1 -Scope sub -Location eastus
31 . EXAMPLE
32 .\validate-deployment.ps1 -Scope group -ResourceGroup my-rg `
33 -Template ./infra/main.bicep -Parameters ./infra/main.parameters.json
34 #>
35 param (
36 [ ValidateSet ( "sub" , "group" )][ string ]$Scope ,
37 [ string ]$Location ,
38 [ string ]$ResourceGroup ,
39 [ string ]$Template = "./infra/main.bicep" ,
40 [ string ]$Parameters = "./infra/main.parameters.json" ,
41 [ string ]$Subscription
42 )
43
44 # Validate arguments
45 if ( -not $Scope) {
46 Write-Error "-Scope is required and must be 'sub' or 'group'."
47 exit 2
48 }
49 if ($Scope -eq "sub" -and -not $Location) {
50 Write-Error "-Location is required when -Scope is 'sub'."
51 exit 2
52 }
53 if ($Scope -eq "group" -and -not $ResourceGroup) {
54 Write-Error "-ResourceGroup is required when -Scope is 'group'."
55 exit 2
56 }
57
58 # Build shared argument arrays
59 $subArgs = @ ()
60 if ($Subscription) { $subArgs = @ ( "--subscription" , $Subscription) }
61
62 $paramArgs = @ ()
63 if ( Test-Path $Parameters) {
64 $paramArgs = @ ( "--parameters" , $Parameters)
65 } else {
66 Write-Host "NOTE: parameters file ' $Parameters ' not found; validating without --parameters."
67 }
68
69 if ($Scope -eq "sub" ) {
70 $scopeTargetArgs = @ ( "--location" , $Location)
71 $scopeDesc = "subscription (location: $Location )"
72 } else {
73 $scopeTargetArgs = @ ( "--resource-group" , $ResourceGroup)
74 $scopeDesc = "resource group ' $ResourceGroup '"
75 }
76
77 # Track overall result (0 = all passed, 1 = at least one failure).
78 $overall = 0
79
80 Write-Host "=== Azure deployment validation ==="
81 Write-Host "Template: $Template "
82 Write-Host "Scope: $scopeDesc "
83 Write-Host ""
84
85 # Step 1: Azure CLI installed
86 Write-Host "--- Step 1: Azure CLI installed (az version) ---"
87 az version *> $null
88 if ( $LASTEXITCODE -eq 0 ) {
89 Write-Host "PASS: Azure CLI is installed."
90 } else {
91 Write-Host "FAIL: Azure CLI not found. Install it, then re-run."
92 Write-Host ""
93 Write-Host "OVERALL: FAIL"
94 exit 1
95 }
96 Write-Host ""
97
98 # Step 2: Authenticated
99 Write-Host "--- Step 2: Authenticated (az account show) ---"
100 $accountName = az account show @subArgs -- query name - o tsv 2> $null
101 if ( $LASTEXITCODE -eq 0 ) {
102 Write-Host "PASS: Authenticated (subscription: $accountName )."
103 } else {
104 Write-Host "FAIL: Not logged in. Run 'az login' (and 'az account set --subscription <id>')."
105 $overall = 1
106 }
107 Write-Host ""
108
109 # Step 3: Bicep compilation
110 Write-Host "--- Step 3: Bicep compilation (az bicep build) ---"
111 $buildOutput = az bicep build -- file $Template 2>&1
112 if ( $LASTEXITCODE -eq 0 ) {
113 Write-Host "PASS: Template compiles cleanly."
114 } else {
115 Write-Host "FAIL: Bicep compilation errors:"
116 Write-Host ($buildOutput | Out-String )
117 $overall = 1
118 }
119 Write-Host ""
120
121 # Step 4: Template validation
122 Write-Host "--- Step 4: Template validation (az deployment $Scope validate) ---"
123 $validateOutput = az deployment $Scope validate @scopeTargetArgs -- template - file $Template @paramArgs @subArgs 2>&1
124 if ( $LASTEXITCODE -eq 0 ) {
125 Write-Host "PASS: Template validated against the target scope."
126 } else {
127 Write-Host "FAIL: Template validation errors:"
128 Write-Host ($validateOutput | Out-String )
129 $overall = 1
130 }
131 Write-Host ""
132
133 # Step 5: What-if preview
134 Write-Host "--- Step 5: What-if preview (az deployment $Scope what-if) ---"
135 $whatifOutput = az deployment $Scope what - if @scopeTargetArgs -- template - file $Template @paramArgs @subArgs 2>&1
136 if ( $LASTEXITCODE -eq 0 ) {
137 $lines = $whatifOutput | Out-String - Stream
138 $createCount = ($lines | Where-Object { $_ -match '^\s*\+ ' }).Count
139 $modifyCount = ($lines | Where-Object { $_ -match '^\s*~ ' }).Count
140 $deleteCount = ($lines | Where-Object { $_ -match '^\s*- ' }).Count
141 Write-Host "PASS: What-if completed. Changes -> Create: $createCount , Modify: $modifyCount , Delete: $deleteCount "
142 } else {
143 Write-Host "FAIL: What-if errors:"
144 Write-Host ($whatifOutput | Out-String )
145 $overall = 1
146 }
147 Write-Host ""
148
149 # Overall result (drives the exit code)
150 if ($overall -eq 0 ) {
151 Write-Host "OVERALL: PASS"
152 } else {
153 Write-Host "OVERALL: FAIL"
154 }
155 exit $overall