param( [switch]$vb, [switch]$hp ) function Show-Help { @" sf-check.ps1 — verify Salesforce CLI environment and configuration USAGE: sf-check.ps1 [-vb] [-hp] OPTIONS: -vb Verbose output (show detailed information) -hp Help - show this help DESCRIPTION: This script verifies that the Salesforce CLI is properly installed and configured. It checks for: - SF CLI installation and version - Available orgs and authentication status - Basic configuration settings - Common issues and recommendations EXAMPLES: sf-check.ps1 # Basic environment check sf-check.ps1 -vb # Verbose output with detailed information "@ } # Function to print status messages with colors function Write-Status { param( [string]$Status, [string]$Message ) switch ($Status) { "OK" { Write-Host "[" -NoNewline Write-Host "✓" -ForegroundColor Green -NoNewline Write-Host "] $Message" } "WARN" { Write-Host "[" -NoNewline Write-Host "!" -ForegroundColor Yellow -NoNewline Write-Host "] $Message" } "ERROR" { Write-Host "[" -NoNewline Write-Host "✗" -ForegroundColor Red -NoNewline Write-Host "] $Message" } "INFO" { Write-Host "[" -NoNewline Write-Host "i" -ForegroundColor Blue -NoNewline Write-Host "] $Message" } } } # Function to check if a command exists function Test-Command { param([string]$CommandName) try { Get-Command $CommandName -ErrorAction Stop | Out-Null return $true } catch { return $false } } # Function to check SF CLI installation function Test-SfInstallation { Write-Status "INFO" "Checking Salesforce CLI installation..." if (Test-Command "sf") { try { $sfVersion = & sf --version 2>$null | Select-Object -First 1 if ([string]::IsNullOrEmpty($sfVersion)) { $sfVersion = "unknown" } Write-Status "OK" "Salesforce CLI found: $sfVersion" if ($Verbose) { $sfPath = (Get-Command sf).Source Write-Host " Location: $sfPath" } return $true } catch { Write-Status "ERROR" "Salesforce CLI found but unable to get version" return $false } } else { Write-Status "ERROR" "Salesforce CLI (sf) not found in PATH" Write-Host " Please install the Salesforce CLI from: https://developer.salesforce.com/tools/sfdxcli" return $false } } # Function to check authenticated orgs function Test-Orgs { Write-Status "INFO" "Checking authenticated orgs..." if (-not (Test-Command "sf")) { return $false } try { $orgListJson = & sf org list --json 2>$null if ($LASTEXITCODE -eq 0) { $orgList = $orgListJson | ConvertFrom-Json $orgCount = $orgList.result.Count if ($orgCount -gt 0) { Write-Status "OK" "Found $orgCount authenticated org(s)" if ($Verbose) { foreach ($org in $orgList.result) { $displayName = if ($org.alias) { $org.alias } else { $org.username } $orgIdShort = $org.orgId.Substring(0, 15) Write-Host " - $displayName ($orgIdShort...)" } } } else { Write-Status "WARN" "No authenticated orgs found" Write-Host " Run 'sf org login web' or 'sf org login jwt' to authenticate" } return $true } else { Write-Status "ERROR" "Unable to list orgs (sf org list failed)" return $false } } catch { Write-Status "ERROR" "Error checking authenticated orgs: $($_.Exception.Message)" return $false } } # Function to check default org function Test-DefaultOrg { Write-Status "INFO" "Checking default org configuration..." if (-not (Test-Command "sf")) { return $false } try { $configJson = & sf config get target-org --json 2>$null if ($LASTEXITCODE -eq 0) { $config = $configJson | ConvertFrom-Json $defaultOrg = $config.result[0].value if ($defaultOrg -and $defaultOrg -ne "null") { Write-Status "OK" "Default org set: $defaultOrg" } else { Write-Status "WARN" "No default org configured" Write-Host " Set with: sf config set target-org " } } else { Write-Status "WARN" "Unable to check default org configuration" } } catch { Write-Status "WARN" "Unable to check default org configuration" } } # Function to check plugins function Test-Plugins { if (-not $Verbose) { return } Write-Status "INFO" "Checking installed plugins..." if (-not (Test-Command "sf")) { return } try { $pluginsJson = & sf plugins --json 2>$null if ($LASTEXITCODE -eq 0) { $plugins = $pluginsJson | ConvertFrom-Json if ($plugins.result.Count -gt 0) { foreach ($plugin in $plugins.result) { Write-Host " - $($plugin.name)" } } else { Write-Host " No additional plugins installed" } } else { Write-Host " Unable to list plugins" } } catch { Write-Host " Unable to list plugins" } } # Function to check system requirements function Test-System { if (-not $Verbose) { return } Write-Status "INFO" "System information..." $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem Write-Host " OS: $($osInfo.Caption) $($osInfo.Version)" Write-Host " PowerShell: $($PSVersionTable.PSVersion)" if (Test-Command "node") { try { $nodeVersion = & node --version 2>$null Write-Host " Node.js: $nodeVersion" } catch { Write-Status "WARN" "Node.js found but unable to get version" } } else { Write-Status "WARN" "Node.js not found (required for some SF CLI features)" } if (Test-Command "git") { try { $gitVersion = & git --version 2>$null Write-Host " Git: $gitVersion" } catch { Write-Status "WARN" "Git found but unable to get version" } } else { Write-Status "WARN" "Git not found (recommended for source control)" } } # Function to run diagnostics function Test-Diagnostics { Write-Status "INFO" "Running SF CLI diagnostics..." if (-not (Test-Command "sf")) { return } try { $null = & sf doctor --json 2>$null if ($LASTEXITCODE -eq 0) { Write-Status "OK" "SF CLI doctor check passed" } else { Write-Status "WARN" "SF CLI doctor check had issues" if ($Verbose) { Write-Host " Run 'sf doctor' for detailed diagnostics" } } } catch { Write-Status "WARN" "SF CLI doctor check had issues" if ($Verbose) { Write-Host " Run 'sf doctor' for detailed diagnostics" } } } # Show help if requested if ($hp) { Show-Help exit 0 } # Set verbose flag $Verbose = $vb # Main execution function Main { Write-Host "🔍 Salesforce CLI Environment Check" -ForegroundColor Cyan Write-Host "==================================" -ForegroundColor Cyan Write-Host "" $hasErrors = $false # Core checks if (-not (Test-SfInstallation)) { $hasErrors = $true } if (-not (Test-Orgs)) { $hasErrors = $true } Test-DefaultOrg # Additional checks for verbose mode if ($Verbose) { Write-Host "" Test-Plugins Write-Host "" Test-System Write-Host "" Test-Diagnostics } Write-Host "" Write-Host "==================================" -ForegroundColor Cyan if ($hasErrors) { Write-Status "ERROR" "Some critical issues found. Please address them before using the SF CLI wrappers." exit 1 } else { Write-Status "OK" "Environment check completed successfully!" Write-Host "" Write-Host "Your Salesforce CLI environment is ready to use with sf-cli-wrapper scripts." Write-Host "Available commands: sf-deploy.ps1, sf-dry-run.ps1, sf-web-open.ps1" if (-not $Verbose) { Write-Host "" Write-Host "Run 'sf-check.ps1 -vb' for detailed system information." } } } # Run main function Main