fixed ps scripts
This commit is contained in:
@@ -8,35 +8,35 @@
|
||||
A user-friendly wrapper around 'sf data export' that simplifies data export
|
||||
from Salesforce orgs with SOQL query support, multiple formats, and intelligent defaults.
|
||||
|
||||
.PARAMETER Query
|
||||
SOQL query to export data (alias: -qy)
|
||||
.PARAMETER qy
|
||||
SOQL query to export data
|
||||
|
||||
.PARAMETER File
|
||||
File containing SOQL query (alias: -fl)
|
||||
.PARAMETER fl
|
||||
File containing SOQL query
|
||||
|
||||
.PARAMETER SObject
|
||||
Standard object query (exports common fields) (alias: -so)
|
||||
.PARAMETER so
|
||||
Standard object query (exports common fields)
|
||||
|
||||
.PARAMETER Output
|
||||
Output file path (default: export.csv) (alias: -ot)
|
||||
.PARAMETER ot
|
||||
Output file path (default: export.csv)
|
||||
|
||||
.PARAMETER TargetOrg
|
||||
Target org username or alias (alias: -to)
|
||||
.PARAMETER to
|
||||
Target org username or alias
|
||||
|
||||
.PARAMETER Format
|
||||
Output format: csv, json (default: csv) (alias: -fm)
|
||||
.PARAMETER fm
|
||||
Output format: csv, json (default: csv)
|
||||
|
||||
.PARAMETER Bulk
|
||||
Use bulk API for large datasets (alias: -bk)
|
||||
.PARAMETER bk
|
||||
Use bulk API for large datasets
|
||||
|
||||
.PARAMETER Wait
|
||||
Wait time in minutes (default: 10) (alias: -wt)
|
||||
.PARAMETER wt
|
||||
Wait time in minutes (default: 10)
|
||||
|
||||
.PARAMETER VerboseOutput
|
||||
Enable verbose output (alias: -ve)
|
||||
.PARAMETER ve
|
||||
Enable verbose output
|
||||
|
||||
.PARAMETER Help
|
||||
Show this help message (alias: -hp)
|
||||
.PARAMETER hp
|
||||
Show this help message
|
||||
|
||||
.EXAMPLE
|
||||
.\sf-data-export.ps1 -qy "SELECT Id, Name FROM Account LIMIT 100"
|
||||
@@ -50,71 +50,21 @@
|
||||
#>
|
||||
|
||||
param(
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("qy")]
|
||||
[string]$Query,
|
||||
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("fl")]
|
||||
[string]$File,
|
||||
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("so")]
|
||||
[string]$SObject,
|
||||
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("ot")]
|
||||
[string]$Output = "export.csv",
|
||||
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("to")]
|
||||
[string]$TargetOrg,
|
||||
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[string]$qy,
|
||||
[string]$fl,
|
||||
[string]$so,
|
||||
[string]$ot = "export.csv",
|
||||
[string]$to,
|
||||
[ValidateSet("csv", "json")]
|
||||
[Alias("fm")]
|
||||
[string]$Format = "csv",
|
||||
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("bk")]
|
||||
[switch]$Bulk,
|
||||
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("wt")]
|
||||
[int]$Wait = 10,
|
||||
|
||||
[Parameter(ParameterSetName="Query")]
|
||||
[Parameter(ParameterSetName="File")]
|
||||
[Parameter(ParameterSetName="SObject")]
|
||||
[Parameter(ParameterSetName="Help")]
|
||||
[Alias("ve")]
|
||||
[switch]$VerboseOutput,
|
||||
|
||||
[Parameter(ParameterSetName="Help", Mandatory=$true)]
|
||||
[Alias("hp")]
|
||||
[switch]$Help
|
||||
[string]$fm = "csv",
|
||||
[switch]$bk,
|
||||
[int]$wt = 10,
|
||||
[switch]$ve,
|
||||
[switch]$hp
|
||||
)
|
||||
|
||||
# Show help if requested
|
||||
if ($Help) {
|
||||
# Show help if no parameters provided or help requested
|
||||
if ($hp -or (-not $qy -and -not $fl -and -not $so)) {
|
||||
Get-Help $MyInvocation.MyCommand.Path -Detailed
|
||||
exit 0
|
||||
}
|
||||
@@ -196,39 +146,39 @@ if (-not (Test-SalesforceCLI)) {
|
||||
}
|
||||
|
||||
# Validate that exactly one query method is specified
|
||||
$queryMethods = @($Query, $File, $SObject | Where-Object { $_ }).Count
|
||||
$queryMethods = @($qy, $fl, $so | Where-Object { $_ }).Count
|
||||
if ($queryMethods -eq 0) {
|
||||
Write-Host "Error: Must specify one of: -Query, -File, or -SObject" -ForegroundColor Red
|
||||
Write-Host "Error: Must specify one of: -qy, -fl, or -so" -ForegroundColor Red
|
||||
Write-Host ""
|
||||
Write-Host "Usage examples:" -ForegroundColor Yellow
|
||||
Write-Host " .\sf-data-export.ps1 -Query `"SELECT Id, Name FROM Account`"" -ForegroundColor Gray
|
||||
Write-Host " .\sf-data-export.ps1 -File queries/accounts.soql" -ForegroundColor Gray
|
||||
Write-Host " .\sf-data-export.ps1 -SObject Account" -ForegroundColor Gray
|
||||
Write-Host " .\sf-data-export.ps1 -qy `"SELECT Id, Name FROM Account`"" -ForegroundColor Gray
|
||||
Write-Host " .\sf-data-export.ps1 -fl queries/accounts.soql" -ForegroundColor Gray
|
||||
Write-Host " .\sf-data-export.ps1 -so Account" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "Use -Help for detailed usage information." -ForegroundColor Yellow
|
||||
Write-Host "Use -hp for detailed usage information." -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($queryMethods -gt 1) {
|
||||
Write-Host "Error: Can only specify one of: -Query, -File, or -SObject" -ForegroundColor Red
|
||||
Write-Host "Error: Can only specify one of: -qy, -fl, or -so" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Determine the final query
|
||||
$finalQuery = ""
|
||||
if ($Query) {
|
||||
$finalQuery = $Query
|
||||
if ($qy) {
|
||||
$finalQuery = $qy
|
||||
Write-Host "Using inline query" -ForegroundColor Green
|
||||
} elseif ($File) {
|
||||
if (-not (Test-Path $File)) {
|
||||
Write-Host "Error: Query file not found: $File" -ForegroundColor Red
|
||||
} elseif ($fl) {
|
||||
if (-not (Test-Path $fl)) {
|
||||
Write-Host "Error: Query file not found: $fl" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
$finalQuery = Get-Content $File -Raw
|
||||
Write-Host "Using query from file: $File" -ForegroundColor Green
|
||||
} elseif ($SObject) {
|
||||
$finalQuery = New-SObjectQuery $SObject
|
||||
Write-Host "Using standard query for $SObject" -ForegroundColor Green
|
||||
$finalQuery = Get-Content $fl -Raw
|
||||
Write-Host "Using query from file: $fl" -ForegroundColor Green
|
||||
} elseif ($so) {
|
||||
$finalQuery = New-SObjectQuery $so
|
||||
Write-Host "Using standard query for $so" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# Validate the query
|
||||
@@ -237,30 +187,30 @@ if (-not (Test-SOQLQuery $finalQuery)) {
|
||||
}
|
||||
|
||||
# Build the sf command based on bulk vs regular query
|
||||
if ($Bulk) {
|
||||
if ($bk) {
|
||||
# Use Bulk API 2.0 for large datasets
|
||||
$sfArgs = @("data", "export", "bulk", "--query", $finalQuery, "--output-file", $Output, "--result-format", $Format)
|
||||
$sfArgs = @("data", "export", "bulk", "--query", $finalQuery, "--output-file", $ot, "--result-format", $fm)
|
||||
|
||||
if ($Wait -ne 10) {
|
||||
if ($wt -ne 10) {
|
||||
$sfArgs += "--wait"
|
||||
$sfArgs += $Wait.ToString()
|
||||
$sfArgs += $wt.ToString()
|
||||
}
|
||||
|
||||
Write-Host "Using Bulk API 2.0" -ForegroundColor Yellow
|
||||
} else {
|
||||
# Use regular data query for smaller datasets
|
||||
$sfArgs = @("data", "query", "--query", $finalQuery, "--output-file", $Output, "--result-format", $Format)
|
||||
$sfArgs = @("data", "query", "--query", $finalQuery, "--output-file", $ot, "--result-format", $fm)
|
||||
}
|
||||
|
||||
# Add optional parameters
|
||||
if ($TargetOrg) {
|
||||
if ($to) {
|
||||
$sfArgs += "--target-org"
|
||||
$sfArgs += $TargetOrg
|
||||
Write-Host "Target org: $TargetOrg" -ForegroundColor Cyan
|
||||
$sfArgs += $to
|
||||
Write-Host "Target org: $to" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
Write-Host "Output format: $Format" -ForegroundColor Cyan
|
||||
Write-Host "Output file: $Output" -ForegroundColor Cyan
|
||||
Write-Host "Output format: $fm" -ForegroundColor Cyan
|
||||
Write-Host "Output file: $ot" -ForegroundColor Cyan
|
||||
|
||||
# Note: sf data commands don't support --verbose flag
|
||||
# VerboseOutput only affects the script's own output (query preview)
|
||||
@@ -271,7 +221,7 @@ Write-Host "📊 Starting Data Export" -ForegroundColor Blue
|
||||
Write-Host "=======================" -ForegroundColor Blue
|
||||
|
||||
# Show query preview if verbose
|
||||
if ($VerboseOutput) {
|
||||
if ($ve) {
|
||||
Write-Host ""
|
||||
Write-Host "📝 SOQL Query:" -ForegroundColor Yellow
|
||||
Write-Host "----------------------------------------" -ForegroundColor Gray
|
||||
@@ -294,8 +244,8 @@ try {
|
||||
Write-Host "✅ Data export completed successfully!" -ForegroundColor Green
|
||||
|
||||
# Show file information if it exists
|
||||
if (Test-Path $Output) {
|
||||
$fileInfo = Get-Item $Output
|
||||
if (Test-Path $ot) {
|
||||
$fileInfo = Get-Item $ot
|
||||
$fileSize = if ($fileInfo.Length -gt 1MB) {
|
||||
"{0:N1} MB" -f ($fileInfo.Length / 1MB)
|
||||
} elseif ($fileInfo.Length -gt 1KB) {
|
||||
@@ -304,16 +254,16 @@ try {
|
||||
"$($fileInfo.Length) bytes"
|
||||
}
|
||||
|
||||
if ($Format -eq "csv") {
|
||||
if ($fm -eq "csv") {
|
||||
# Count records (excluding header)
|
||||
$recordCount = (Get-Content $Output).Count - 1
|
||||
Write-Host "📁 Exported $recordCount records to $Output ($fileSize)" -ForegroundColor Cyan
|
||||
$recordCount = (Get-Content $ot).Count - 1
|
||||
Write-Host "📁 Exported $recordCount records to $ot ($fileSize)" -ForegroundColor Cyan
|
||||
} else {
|
||||
Write-Host "📁 Data exported to $Output ($fileSize)" -ForegroundColor Cyan
|
||||
Write-Host "📁 Data exported to $ot ($fileSize)" -ForegroundColor Cyan
|
||||
}
|
||||
}
|
||||
|
||||
if ($VerboseOutput) {
|
||||
if ($ve) {
|
||||
Write-Host "💡 Use a spreadsheet application or text editor to view the exported data" -ForegroundColor Yellow
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user