Private/Test-GhostPreviousPath.ps1
|
function Test-GhostPreviousPath { <# .SYNOPSIS Stops a Sync command that was given a PreviousPath it cannot work with. .DESCRIPTION A Sync command that is given a PreviousPath removes what a previous file describes and no current file describes any more. That is only right when the command knows every current file, so this check runs before anything is sent. The current files have to be in the parameter Path. Files from the pipeline arrive one at a time and can be a part of the files only. Everything else would be taken for removed. Every previous path has to exist. A path that is wrong would make the command remove nothing without saying so. Without a PreviousPath nothing is checked. .EXAMPLE Test-GhostPreviousPath -BoundParameters $PSBoundParameters Returns nothing when the parameters can be used, and throws with the reason when they cannot. #> [CmdletBinding()] [OutputType([void])] param( # The parameters the Sync command was called with. [Parameter(Mandatory)] [System.Collections.IDictionary] $BoundParameters ) if ($BoundParameters.Keys -notcontains 'PreviousPath') { return } if ($BoundParameters.Keys -notcontains 'Path') { $reason = 'Files from the pipeline can be a part of the files only, and the rest would be removed.' throw "With PreviousPath, pass every current file or folder to the parameter Path. $reason" } foreach ($item in $BoundParameters['PreviousPath']) { if (-not (Test-Path -LiteralPath $item) -and -not (Test-Path -Path $item)) { throw "The previous path '$item' does not exist." } } } |