Public/Export-Biller.ps1
|
function Export-Biller { <# .SYNOPSIS Exports billers to a file. .DESCRIPTION Exports one or all billers from a budget to CSV or JSON format. .PARAMETER OutputPath The file path for the export. Extension determines format (.csv or .json). .PARAMETER Name Optional biller name to export. If omitted, exports all billers. .PARAMETER Format Output format: CSV or JSON. If not specified, determined by file extension. .PARAMETER Budget Optional budget name to target. Uses active budget if not specified. .PARAMETER DataPath Optional custom path for data storage. Overrides budget-based paths. .EXAMPLE Export-Biller -OutputPath "billers.csv" Exports all billers to a CSV file. .EXAMPLE Export-Biller -Name "Netflix" -OutputPath "netflix.json" Exports a specific biller to JSON format. .EXAMPLE Export-Biller -OutputPath "C:\Backups\billers.json" -Format JSON -Budget "MyFamilyBudget" Exports all billers from a specific budget to JSON. .OUTPUTS File path of the exported data #> [CmdletBinding()] param( [Parameter(Mandatory)] [string]$OutputPath, [Parameter()] [string]$Name, [Parameter()] [ValidateSet('CSV', 'JSON')] [string]$Format, [Parameter()] [string]$Budget, [Parameter()] [string]$DataPath ) # Get billers $getParams = @{} if ($Name) { $getParams['Name'] = $Name } if ($Budget) { $getParams['Budget'] = $Budget } if ($DataPath) { $getParams['DataPath'] = $DataPath } $billers = Get-Biller @getParams if (-not $billers -or $billers.Count -eq 0) { Write-Warning "No billers found to export." return } # Determine format from file extension if not specified if (-not $Format) { $extension = [System.IO.Path]::GetExtension($OutputPath).ToLower() $Format = switch ($extension) { '.json' { 'JSON' } '.csv' { 'CSV' } default { 'CSV' } } } # Ensure directory exists $directory = Split-Path -Path $OutputPath -Parent if ($directory -and -not (Test-Path $directory)) { New-Item -Path $directory -ItemType Directory -Force | Out-Null } # Export based on format try { switch ($Format) { 'CSV' { $billers | Select-Object Id, Name, StartDate, Frequency, Amount, Status, Tags, AccountId | Export-Csv -Path $OutputPath -NoTypeInformation -ErrorAction Stop Write-Verbose "Exported $($billers.Count) biller(s) to CSV: $OutputPath" } 'JSON' { $billers | ConvertTo-Json -Depth 10 | Set-Content -Path $OutputPath -ErrorAction Stop Write-Verbose "Exported $($billers.Count) biller(s) to JSON: $OutputPath" } } return (Resolve-Path $OutputPath).Path } catch { Write-Error "Failed to export billers: $_" } } |