Public/build/Helpers/Backup-Files.ps1
<#
The MIT License (MIT) Copyright (c) 2015 Objectivity Bespoke Software Specialists Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #> function Backup-Files { <# .SYNOPSIS Backups list of files/directories. .PARAMETER Path Paths to the files/directories to backup. .PARAMETER DestinationPath Destination path where the files will be backed up. .EXAMPLE Backup-Files -Path 'FileToBackup.txt' #> [CmdletBinding()] [OutputType([object[]])] param( [Parameter(Mandatory=$true)] [string[]] $Path, [Parameter(Mandatory=$false)] [string] $DestinationPath ) if (!$DestinationPath) { $DestinationPath = 'BuildBackup' } $leaves = Split-Path -Path $Path -Leaf $result = @() Write-Log -Info "Copying path(s) $($leaves -join ', ') to '$DestinationPath'" [void](New-Item -Path $DestinationPath -ItemType Directory -Force) foreach ($p in $Path) { if (!(Test-Path -LiteralPath $p)) { throw "Path '$p' does not exist." } $p = (Resolve-Path -LiteralPath $p).ProviderPath if (Test-Path -LiteralPath $p -PathType Leaf) { $toCopy = $p } else { $toCopy = Get-ChildItem -Path $p -Recurse | Select-Object -ExpandProperty FullName } foreach ($file in $toCopy) { Copy-Item -Path $file -Destination $DestinationPath -Force $result += [PSCustomObject]@{ SourcePath = $file BackupPath = (Join-Path -Path $DestinationPath -ChildPath (Split-Path -Path $file -Leaf)) } } } return $result } |