Public/Get-Budget.ps1
|
function Get-Budget { <# .SYNOPSIS Retrieves budget information. .DESCRIPTION Gets one or more budgets from the workspace. Can show detailed metadata. .PARAMETER Name Get a specific budget by name. .PARAMETER Detailed Include full metadata for each budget. .PARAMETER WorkspacePath Optional custom workspace path. .EXAMPLE Get-Budget Lists all budgets .EXAMPLE Get-Budget -Name "daily-expenses" Gets a specific budget .EXAMPLE Get-Budget -Detailed Lists all budgets with full metadata .OUTPUTS Array of budget objects (PSCustomObject) #> [CmdletBinding(DefaultParameterSetName = 'All')] param( [Parameter(ParameterSetName = 'ByName')] [string]$Name, [Parameter()] [switch]$Detailed, [Parameter()] [string]$WorkspacePath ) # Check if workspace is initialized if (-not (Test-WorkspaceInitialized -WorkspacePath $WorkspacePath)) { Write-Warning "Budget workspace not initialized. Run Initialize-BudgetWorkspace first." return @() } # Get workspace path if (-not $WorkspacePath) { $WorkspacePath = Get-WorkspacePath } $budgetsPath = Join-Path $WorkspacePath 'budgets' # Get budget directories if ($PSCmdlet.ParameterSetName -eq 'ByName') { $budgetPath = Get-BudgetPath -BudgetName $Name -WorkspacePath $WorkspacePath if (-not (Test-Path $budgetPath)) { Write-Error "Budget '$Name' not found" return } $budgetDirs = @(Get-Item $budgetPath) } else { if (-not (Test-Path $budgetsPath)) { return @() } $budgetDirs = Get-ChildItem -Path $budgetsPath -Directory } # Get active budget for reference $preferences = Get-Preferences -WorkspacePath $WorkspacePath $activeBudgetName = $preferences.activeBudget # Build budget objects $budgets = foreach ($dir in $budgetDirs) { $budgetName = $dir.Name if ($Detailed) { $metadata = Get-BudgetMetadata -BudgetPath $dir.FullName if ($metadata) { $metadata | Add-Member -NotePropertyName 'IsActive' -NotePropertyValue ($budgetName -eq $activeBudgetName) -Force $metadata } } else { [PSCustomObject]@{ Name = $budgetName IsActive = ($budgetName -eq $activeBudgetName) Path = $dir.FullName } } } return $budgets } |