#requires -Version 7.0 <# .SYNOPSIS Launches the interactive Conditional Access Policy Wizard (TUI). .DESCRIPTION Starts the Python-based TUI wizard that guides you through tenant, user, admin, guest, and application policy choices. The wizard generates a deployment-ready YAML baseline using the structured naming convention. Automatically locates the project venv or system Python with the required packages (rich, pyyaml). .EXAMPLE ./Scripts/Start-CAWizard.ps1 #> [CmdletBinding()] param() $ErrorActionPreference = 'Stop' $wizardPath = Join-Path $PSScriptRoot 'ca-wizard.py' if (-not (Test-Path $wizardPath)) { throw "Wizard script not found: $wizardPath" } # ===================================================================== # Resolve Python interpreter # ===================================================================== function Test-PythonPackages { param([string]$PyExe) if (-not $PyExe) { return $false } try { $result = & $PyExe -c "import rich, yaml" 2>&1 return ($LASTEXITCODE -eq 0) } catch { return $false } } $candidates = [System.Collections.Generic.List[string]]::new() # 1. Project venv (Linux/macOS) $venvPy = Join-Path (Split-Path $PSScriptRoot -Parent) '.venv-pdf/bin/python3' if (Test-Path $venvPy) { $candidates.Add($venvPy) } # 2. Project venv (Windows) $venvPyWin = Join-Path (Split-Path $PSScriptRoot -Parent) '.venv-pdf/Scripts/python.exe' if (Test-Path $venvPyWin) { $candidates.Add($venvPyWin) } # 3. Common system commands foreach ($cmd in @('python3', 'python')) { $found = Get-Command $cmd -ErrorAction SilentlyContinue if ($found) { $candidates.Add($found.Source) } } $pythonPath = $null foreach ($c in $candidates) { if (Test-PythonPackages -PyExe $c) { $pythonPath = $c break } } # If nothing has the packages, try installing into the venv if (-not $pythonPath) { $venvPy = $candidates | Where-Object { $_ -match '\.venv' } | Select-Object -First 1 if ($venvPy -and (Test-Path $venvPy)) { Write-Host "Installing required packages into venv..." -ForegroundColor Yellow $pip = Join-Path (Split-Path $venvPy -Parent) 'pip' if (-not (Test-Path $pip)) { $pip = Join-Path (Split-Path $venvPy -Parent) 'pip3' } & $pip install rich pyyaml 2>&1 | ForEach-Object { Write-Host " $_" -ForegroundColor DarkGray } if (Test-PythonPackages -PyExe $venvPy) { $pythonPath = $venvPy } } } if (-not $pythonPath) { throw @" Could not find a Python interpreter with 'rich' and 'pyyaml' installed. Please install the requirements: python3 -m pip install rich pyyaml Or activate the project venv manually: source .venv-pdf/bin/activate python3 Scripts/ca-wizard.py "@ } Write-Host "Using Python: $pythonPath" -ForegroundColor DarkGray # ===================================================================== # Run wizard # ===================================================================== & $pythonPath $wizardPath