CmdLets/Library/Initialize-Library.ps1
|
Function Initialize-Library { <# .SYNOPSIS Initializes or updates a global application library from JSON configuration files or directories. .DESCRIPTION The Initialize-Library function parses JSON configuration files containing application definitions and populates a global array (`$Global:AppLibrary`). It features two parameter sets: 1. File: Imports one or more specific `[System.IO.FileInfo]` objects (also accepting the alias 'Library'). 2. Path: Recursively searches specified `[System.IO.DirectoryInfo]` paths for matching `*.json` files. By default, running this function resets the global library. Use the `-Append` switch to add new applications to an existing library without overwriting current entries. Duplicate items (matched by the 'Name' property) are filtered out, and warnings are emitted if duplicates are encountered. .PARAMETER File Specifies an array of FileInfo objects containing application data to import. Can also be provided via the `Library` alias. Part of the 'File' parameter set and accepts pipeline input by property name. .PARAMETER Path Specifies an array of DirectoryInfo objects containing paths to scan recursively for `*.json` configuration files. Part of the 'Path' parameter set and accepts pipeline input by property name. .PARAMETER Append A switch that prevents clearing the existing `$Global:AppLibrary` when set, allowing new applications to be appended to the current global list. .INPUTS System.IO.FileInfo[], System.IO.DirectoryInfo[] You can pipe file information objects or directory information objects directly to this function. .OUTPUTS None This function modifies the `$Global:AppLibrary` global variable in-place. .EXAMPLE # Initialize library from a specific JSON file using the parameter alias Initialize-Library -File "C:\Config\Apps.json" .EXAMPLE # Recursively load all JSON files from a directory path Initialize-Library -Path "C:\Configs" .EXAMPLE # Append applications from a folder to an existing global library Get-Item "C:\AdditionalConfigs" | Initialize-Library -Append #> [CmdletBinding()] Param( [Alias("Library")] [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName="File")] [System.IO.FileInfo[]]$File, [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName="Path")] [System.IO.DirectoryInfo[]]$Path, [Parameter(Mandatory=$False)] [Switch]$Append ) Begin { If (!$Append.IsPresent) { Write-Verbose "Clearing AppLibrary" $Global:AppLibrary = @() } } Process { Switch ($PSCmdlet.ParameterSetName) { "Path" { $Targets = $Path | ForEach-Object { Get-ChildItem (join-Path $_.fullname "*.json") -Recurse } } "File" { $Targets = $File } } Write-Verbose "Targets: $($Targets.Count)" ForEach ($Item in $Targets) { Write-Verbose "Processing: $($Item.Name)" $New = $Item | Get-Content | ConvertFrom-JSON | Where-Object { $_.Name -NotIn $Global:AppLibrary.Name } $Existing = $Item | Get-Content | ConvertFrom-JSON | Where-Object { $_.Name -In $Global:AppLibrary.Name } If ($Existing) { Write-Warning "File $($Item.Name) contains applications that are already in library and not imported" } $Global:AppLibrary += $New | ForEach-Object { Write-Verbose "`tImporting: $($_.Name)"; $_; } } } End { Write-Verbose "Applications: $($Global:AppLibrary.Count)" } } |