scripts/modules/prerequisites/azure-cli/test-azure-cli.ps1
# strangeloop Setup - Azure CLI Testing Module # Version: 1.0.0 param( [switch]$Detailed ) # Import shared modules $SharedPath = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent | Join-Path -ChildPath "shared" Import-Module "$SharedPath\write-functions.ps1" -Force -DisableNameChecking Import-Module "$SharedPath\test-functions.ps1" -Force -DisableNameChecking function Test-AzureCLI { param( [switch]$Detailed ) Write-Step "Testing Azure CLI installation..." # First try normal command test if (-not (Test-Command "az")) { # If not found, try refreshing PATH from registry (Azure CLI might be installed but PATH not updated) Write-Info "Azure CLI not found in current PATH, refreshing environment..." try { # Refresh PATH from registry $machinePath = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment").GetValue("PATH", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $userPath = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Environment").GetValue("PATH", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $env:PATH = $machinePath + ";" + $userPath # Wait a moment for PATH to take effect Start-Sleep -Seconds 2 # Try again after PATH refresh if (-not (Test-Command "az")) { Write-Error "Azure CLI command 'az' not found" return $false } else { Write-Info "Azure CLI found after PATH refresh" } } catch { Write-Warning "Could not refresh PATH from registry: $($_.Exception.Message)" Write-Error "Azure CLI command 'az' not found" return $false } } $version = Get-ToolVersion "az" if (-not $version) { Write-Error "Could not determine Azure CLI version" return $false } Write-Success "Azure CLI $version is properly installed" # Test Azure CLI functionality try { $versionOutput = az version --output json 2>$null if ($versionOutput) { Write-Success "Azure CLI functionality test passed" if ($Detailed) { # Test Azure CLI extensions $extensions = az extension list --output json 2>$null | ConvertFrom-Json if ($extensions) { Write-Info "Azure CLI extensions installed: $($extensions.Count)" foreach ($ext in $extensions | Select-Object -First 3) { Write-Info " - $($ext.name) ($($ext.version))" } } else { Write-Info "No Azure CLI extensions installed" } # Test login status try { $account = az account show --output json 2>$null | ConvertFrom-Json if ($account) { Write-Success "Azure CLI is logged in as: $($account.user.name)" Write-Info "Current subscription: $($account.name)" } else { Write-Info "Azure CLI is not currently logged in" } } catch { Write-Info "Azure CLI is not currently logged in" } } return $true } else { Write-Warning "Azure CLI functionality test failed" return $false } } catch { Write-Warning "Azure CLI functionality test failed: $($_.Exception.Message)" return $false } } # Main execution if ($MyInvocation.InvocationName -ne '.') { $result = Test-AzureCLI -Detailed:$Detailed if ($result) { Write-Success "Azure CLI testing completed successfully" exit 0 } else { Write-Error "Azure CLI testing failed" exit 1 } } # Export functions for module usage Export-ModuleMember -Function @( 'Test-AzureCLI' ) |