#!/usr/bin/env pwsh <# .SYNOPSIS Quick org information display wrapper for Salesforce CLI .DESCRIPTION A user-friendly wrapper around Salesforce CLI org commands that provides quick access to org information, limits, and authentication status with clean, formatted output. .PARAMETER TargetOrg Target org username or alias (uses default if not specified) .PARAMETER Limits Show detailed org limits information .PARAMETER ListOrgs List all authenticated orgs .PARAMETER Verbose Enable verbose output with additional details .PARAMETER Help Show this help message .EXAMPLE .\sf-org-info.ps1 .\sf-org-info.ps1 -TargetOrg "production" .\sf-org-info.ps1 -Limits .\sf-org-info.ps1 -ListOrgs .NOTES This script automatically checks for Salesforce CLI installation and runs diagnostics if the CLI is not found. #> param( [string]$TargetOrg, [switch]$Limits, [switch]$ListOrgs, [switch]$Verbose, [switch]$Help ) # Show help if requested if ($Help) { Get-Help $MyInvocation.MyCommand.Path -Detailed exit 0 } # Function to check if Salesforce CLI is installed function Test-SalesforceCLI { try { $null = Get-Command sf -ErrorAction Stop return $true } catch { return $false } } # Function to run sf-check diagnostics function Invoke-SalesforceCheck { $checkScript = if (Test-Path "sf-check.ps1") { ".\sf-check.ps1" } elseif (Test-Path "sf-check.sh") { "bash sf-check.sh" } else { $null } if ($checkScript) { Write-Host "Running Salesforce CLI diagnostics..." -ForegroundColor Yellow Invoke-Expression $checkScript } else { Write-Host "Salesforce CLI not found and no diagnostic script available." -ForegroundColor Red Write-Host "Please install the Salesforce CLI: https://developer.salesforce.com/tools/salesforcecli" -ForegroundColor Red } } # Function to execute sf commands with error handling function Invoke-SafeSfCommand { param([string[]]$Arguments) try { Write-Host "Executing: sf $($Arguments -join ' ')" -ForegroundColor Gray Write-Host "" & sf @Arguments $exitCode = $LASTEXITCODE if ($exitCode -ne 0) { Write-Host "" Write-Host "❌ Command failed with exit code: $exitCode" -ForegroundColor Red return $false } return $true } catch { Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red return $false } } # Function to display section header function Write-SectionHeader { param([string]$Title) Write-Host "" Write-Host "🔍 $Title" -ForegroundColor Blue Write-Host ("=" * (4 + $Title.Length)) -ForegroundColor Blue } # Silently check for Salesforce CLI if (-not (Test-SalesforceCLI)) { Invoke-SalesforceCheck exit 1 } # If ListOrgs is specified, show all authenticated orgs and exit if ($ListOrgs) { Write-SectionHeader "Authenticated Orgs" $success = Invoke-SafeSfCommand @("org", "list") if ($success) { Write-Host "" Write-Host "✅ Listed all authenticated orgs" -ForegroundColor Green } exit 0 } # Build the base sf command for org display $sfArgs = @("org", "display") # Add target org if specified if ($TargetOrg) { $sfArgs += "--target-org" $sfArgs += $TargetOrg Write-Host "Target org: $TargetOrg" -ForegroundColor Cyan } # Add verbose flag if requested if ($Verbose) { $sfArgs += "--verbose" } # Display org information Write-SectionHeader "Organization Information" $success = Invoke-SafeSfCommand $sfArgs if (-not $success) { exit 1 } # If limits are requested, show org limits if ($Limits) { Write-SectionHeader "Organization Limits" # Build limits command $limitsArgs = @("org", "list", "limits") if ($TargetOrg) { $limitsArgs += "--target-org" $limitsArgs += $TargetOrg } $success = Invoke-SafeSfCommand $limitsArgs if (-not $success) { Write-Host "Warning: Could not retrieve org limits" -ForegroundColor Yellow } } # Show additional org context if verbose if ($Verbose) { Write-SectionHeader "Additional Context" # Show all authenticated orgs for context Write-Host "📋 All Authenticated Orgs:" -ForegroundColor Yellow $success = Invoke-SafeSfCommand @("org", "list") if ($success) { Write-Host "" } # Try to show current user info Write-Host "👤 Current User Info:" -ForegroundColor Yellow $userArgs = @("org", "display", "user") if ($TargetOrg) { $userArgs += "--target-org" $userArgs += $TargetOrg } $success = Invoke-SafeSfCommand $userArgs } # Final success message Write-Host "" Write-Host "✅ Organization information retrieved successfully!" -ForegroundColor Green if (-not $Limits -and -not $Verbose) { Write-Host "" Write-Host "💡 Tip: Use -Limits to see org limits, -Verbose for more details, or -ListOrgs to see all authenticated orgs" -ForegroundColor Yellow }