Public/Get-ProfileSource.ps1
<#
.SYNOPSIS Retrieves profile paths from a parent directory, a specific path, or a CSV file. .DESCRIPTION This function retrieves profile paths from either a parent directory, a specific profile path, or a CSV file containing paths. It supports importing these paths and returning them as output objects. .PARAMETER ParentPath The parent directory containing profile paths. .PARAMETER ProfilePath A specific profile path. .PARAMETER CSV A CSV file containing profile paths. .EXAMPLE Get-ProfileSource -ParentPath "C:\Profiles" # Checks the parent path and retrieve the profiles .EXAMPLE Get-ProfileSource -ProfilePath "C:\Profiles\Profile1" # Retrieves the specified profile .EXAMPLE Get-ProfileSource -CSV "C:\Profiles\ProfilePaths.csv" # Imports the profiles that are specified in the CSV #> function Get-ProfileSource { [CmdletBinding(SupportsShouldProcess = $True, DefaultParameterSetName = 'ParentPath')] Param ( [Parameter(ValueFromPipelineByPropertyName, Mandatory = $True, ParameterSetName = 'ParentPath')] [string]$ParentPath, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $True, ParameterSetName = 'ProfilePath')] [string]$ProfilePath, [Parameter(ValueFromPipelineByPropertyName, Mandatory = $True, ParameterSetName = 'CSV')] [string]$CSV ) Begin { $OutputObject = @() } Process { if ($PSCmdlet.ParameterSetName -eq 'ParentPath') { if ($PSCmdlet.ShouldProcess($ParentPath, 'Import')) { $PathList = Get-ChildItem -Path $ParentPath -Directory | Select-Object -ExpandProperty FullName foreach ($Path in $PathList) { $Item = [PSCustomObject]@{ ProfilePath = $Path } $OutputObject += $Item } } } if ($PSCmdlet.ParameterSetName -eq 'ProfilePath') { if ($PSCmdlet.ShouldProcess($ProfilePath, 'Import')) { $Item = [PSCustomObject]@{ ProfilePath = (Get-Item -Path $ProfilePath).FullName } $OutputObject += $Item } } if ($PSCmdlet.ParameterSetName -eq 'CSV') { if ($PSCmdlet.ShouldProcess($CSV, 'Import')) { $csvData = Import-Csv -Path $CSV if (!$csvData.Path) { Write-Error "No Path header found in the CSV file." } else { foreach ($Path in ($csvData.Path | Where-Object {$_})) { $Item = [PSCustomObject]@{ ProfilePath = $Path } $OutputObject += $Item } } } } } End { $OutputObject } } |