#!/usr/bin/env pwsh <# .SYNOPSIS Web login wrapper for Salesforce CLI .DESCRIPTION A user-friendly wrapper around 'sf org login web' that simplifies authenticating to Salesforce orgs via web browser with better formatting and error handling. .PARAMETER al Alias for the authenticated org .PARAMETER in Instance URL to authenticate against .PARAMETER ud Update default org after login (sets --set-default) .PARAMETER ve Enable verbose output showing login details .PARAMETER hp Show this help message .EXAMPLE .\sf-web-login.ps1 -al "NUSHUB-PROD" .\sf-web-login.ps1 -al "MySandbox" -in "https://test.my-domain.my.salesforce.com" -ud .\sf-web-login.ps1 -ve .NOTES This script automatically checks for Salesforce CLI installation and runs diagnostics if the CLI is not found. #> param( [string]$al, [string]$in, [switch]$ud, [switch]$ve, [switch]$hp ) # Show help if requested if ($hp) { 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 { Write-Host "❌ Salesforce CLI (sf) not found!" -ForegroundColor Red Write-Host "" Write-Host "Running environment check to help you get started..." -ForegroundColor Yellow Write-Host "" $checkScript = if (Test-Path "sf-check.ps1") { ".\sf-check.ps1" } elseif (Test-Path "sf-check") { "./sf-check" } else { $null } if ($checkScript) { try { Invoke-Expression $checkScript } catch { Write-Host "Error running diagnostics: $($_.Exception.Message)" -ForegroundColor Red } } else { Write-Host "sf-check not found. Please install the Salesforce CLI from:" -ForegroundColor Red Write-Host "https://developer.salesforce.com/tools/sfdxcli" -ForegroundColor Red } } # Silently check for Salesforce CLI if (-not (Test-SalesforceCLI)) { Invoke-SalesforceCheck exit 1 } # Build the sf command $sfArgs = @("org", "login", "web") # Add parameters based on input if ($al) { $sfArgs += "--alias" $sfArgs += $al } if ($in) { $sfArgs += "--instance-url" $sfArgs += $in } if ($ud) { $sfArgs += "--set-default" } # Display verbose information if requested if ($ve) { Write-Host "🔐 Starting Web Login" -ForegroundColor Blue Write-Host "====================" -ForegroundColor Blue if ($al) { Write-Host "Alias: $al" -ForegroundColor Cyan } if ($in) { Write-Host "Instance URL: $in" -ForegroundColor Cyan } if ($ud) { Write-Host "Will set as default org: Yes" -ForegroundColor Cyan } Write-Host "" } # Display the command being run Write-Host ">>> Running: sf $($sfArgs -join ' ')" -ForegroundColor Gray # Execute the command try { $startTime = Get-Date & sf @sfArgs $exitCode = $LASTEXITCODE $endTime = Get-Date $duration = $endTime - $startTime if ($ve) { Write-Host "" Write-Host "⏱️ Login completed in $($duration.TotalSeconds) seconds" -ForegroundColor Green } if ($exitCode -eq 0) { Write-Host "" Write-Host "✅ Web login completed successfully!" -ForegroundColor Green if ($al) { Write-Host "🏷️ Org authenticated with alias: $al" -ForegroundColor Cyan } if ($ud) { Write-Host "⚡ Set as default org" -ForegroundColor Yellow } } else { Write-Host "" Write-Host "❌ Web login failed with exit code: $exitCode" -ForegroundColor Red Write-Host "💡 Check your browser and try again" -ForegroundColor Yellow } exit $exitCode } catch { Write-Host "Error executing sf command: $($_.Exception.Message)" -ForegroundColor Red exit 1 }