99 [string ]$TerraformVersion = " latest" ,
1010 [string ]$RunCheckov = " false" ,
1111 [string ]$TfPlanFileName = " tfplan.plan" ,
12- [string ]$TerraformCodeLocation = " terraform/0_rg" ,
12+ [string ]$TerraformCodeLocation = " terraform" ,
13+ [string []]$TerraformStackToRun = @ (' all' ),
14+ [string ]$CreateTerraformWorkspace = " true" ,
15+ [string ]$TerraformWorkspace = " dev" ,
16+ [string ]$AttemptAzureLogin = " true" ,
1317 [string ]$UseAzureClientSecretLogin = " true" ,
1418 [string ]$UseAzureOidcLogin = " false" ,
1519 [string ]$UseAzureUserLogin = " false" ,
@@ -20,16 +24,11 @@ $ErrorActionPreference = 'Stop'
2024$currentWorkingDirectory = (Get-Location ).path
2125$fullTerraformCodePath = Join-Path - Path $currentWorkingDirectory - ChildPath $TerraformCodeLocation
2226
23- # Get timestamp in "HH:mm:ss" format
24- $timestamp = Get-Date - Format " HH:mm:ss"
25-
26- # # Setup script modules etc
27-
2827# Get script directory
2928$scriptDir = Split-Path - Path $MyInvocation.MyCommand.Definition - Parent
3029
3130# Import all required modules
32- $modules = @ (" Logger" , " Utils" , " AzureCliLogin" , " AzurePwshLogin " , " Nsg " , " Terraform" , " Storage" , " Homebrew" , " Checkov" , " Tenv" , " Choco" )
31+ $modules = @ (" Logger" , " Utils" , " AzureCliLogin" , " Terraform" , " Storage" , " Homebrew" , " Checkov" , " Tenv" , " Choco" )
3332foreach ($module in $modules )
3433{
3534 $modulePath = Join-Path - Path $scriptDir - ChildPath " PowerShellModules/$module .psm1"
@@ -39,18 +38,17 @@ foreach ($module in $modules)
3938 }
4039 else
4140 {
42- Write-Host " ERROR: $timestamp - [$ ( $MyInvocation.MyCommand.Name ) ] Module not found: $modulePath " - ForegroundColor Red
41+ Write-Host " ERROR: [$ ( $MyInvocation.MyCommand.Name ) ] Module not found: $modulePath " - ForegroundColor Red
4342 exit 1
4443 }
4544}
4645
4746# Log that modules were loaded
48- _LogMessage - Level " INFO" - Message " $timestamp - [$ ( $MyInvocation.MyCommand.Name ) ] Modules loaded successfully" - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
47+ _LogMessage - Level " INFO" - Message " [$ ( $MyInvocation.MyCommand.Name ) ] Modules loaded successfully" - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
4948
5049$convertedDebugMode = ConvertTo-Boolean $DebugMode
5150_LogMessage - Level ' DEBUG' - Message " DebugMode: `" $DebugMode `" → $convertedDebugMode " - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
5251
53-
5452# Enable debug mode if DebugMode is set to $true
5553if ($true -eq $convertedDebugMode )
5654{
8583
8684 # Convert the string flags to Boolean and log the results at DEBUG level
8785
86+ $convertedAttemptAzureLogin = ConvertTo-Boolean $AttemptAzureLogin
87+ _LogMessage - Level ' DEBUG' - Message " AttemptAzureLogin: `" $AttemptAzureLogin `" → $convertedAttemptAzureLogin " - InvocationName $MyInvocation.MyCommand.Name
88+
8889 $convertedUseAzureClientSecretLogin = ConvertTo-Boolean $UseAzureClientSecretLogin
8990 _LogMessage - Level ' DEBUG' - Message " UseAzureClientSecretLogin: `" $UseAzureClientSecretLogin `" → $convertedUseAzureClientSecretLogin " - InvocationName $MyInvocation.MyCommand.Name
9091
118119 $convertedRunCheckov = ConvertTo-Boolean $RunCheckov
119120 _LogMessage - Level ' DEBUG' - Message " RunCheckov: `" $RunCheckov `" → $convertedRunCheckov " - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
120121
122+ $convertedCreateTerraformWorkspace = ConvertTo-Boolean $CreateTerraformWorkspace
123+ _LogMessage - Level ' DEBUG' - Message " CreateTerraformWorkspace: `" $CreateTerraformWorkspace `" → $convertedCreateTerraformWorkspace " - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
124+
125+
121126 # ── Chicken-and-egg / mutual exclusivity checks ───────────────────────────────
122127 if (-not $convertedRunTerraformInit -and (
123128 $convertedRunTerraformPlan -or
@@ -160,11 +165,31 @@ try
160165 try
161166 {
162167
163- Connect-AzureCli `
164- - UseClientSecret $convertedUseAzureClientSecretLogin `
165- - UseOidc $convertedUseAzureOidcLogin `
166- - UseUserDeviceCode $convertedUseAzureUserLogin `
167- - UseManagedIdentity $convertedUseAzureManagedIdentityLogin
168+ if ($convertedAttemptAzureLogin )
169+ {
170+
171+ Connect-AzureCli `
172+ - UseClientSecret $convertedUseAzureClientSecretLogin `
173+ - UseOidc $convertedUseAzureOidcLogin `
174+ - UseUserDeviceCode $convertedUseAzureUserLogin `
175+ - UseManagedIdentity $convertedUseAzureManagedIdentityLogin
176+ }
177+
178+ $stackFolders = Get-TerraformStackFolders `
179+ - CodeRoot $fullTerraformCodePath `
180+ - StacksToRun $TerraformStackToRun
181+
182+ foreach ($folder in $stackFolders )
183+ {
184+ # Example: validate + fmt-check for each stack
185+ Invoke-TerraformFmtCheck - CodePath $folder
186+ Invoke-TerraformInit - CodePath $folder
187+ if ($convertedCreateTerraformWorkspace -and -not [string ]::IsNullOrWhiteSpace($TerraformWorkspace ))
188+ {
189+ Invoke-TerraformWorkspaceSelect - CodePath $folder - WorkspaceName $TerraformWorkspace
190+ }
191+ Invoke-TerraformValidate - CodePath $folder
192+ }
168193 }
169194 catch
170195 {
174199}
175200catch
176201{
177- _LogMessage - Level " ERROR" - Message " $timestamp - [ $ ( $MyInvocation .MyCommand.Name ) ] Error: $ ( $_.Exception.Message ) " - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
202+ _LogMessage - Level " ERROR" - Message " Error: $ ( $_.Exception.Message ) " - InvocationName " $ ( $MyInvocation.MyCommand.Name ) "
178203 exit 1
179204}
180205
@@ -215,6 +240,7 @@ finally
215240 Disconnect-AzureCli - IsUserDeviceLogin $false
216241 }
217242
243+ $Env: TF_LOG = $null
218244 Set-Location $currentWorkingDirectory
219245}
220246
0 commit comments