| 1 |
# Copyright (c): 2018, Dag Wieers (@dagwieers) <[email protected]> |
| 2 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
|
| 4 |
Function Backup-File { |
| 5 |
<# |
| 6 |
.SYNOPSIS |
| 7 |
Helper function to make a backup of a file. |
| 8 |
.EXAMPLE |
| 9 |
Backup-File -path $path -WhatIf:$check_mode |
| 10 |
#> |
| 11 |
[CmdletBinding(SupportsShouldProcess = $true)] |
| 12 |
|
| 13 |
Param ( |
| 14 |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| 15 |
[string] $path |
| 16 |
) |
| 17 |
|
| 18 |
Process { |
| 19 |
$backup_path = $null |
| 20 |
if (Test-Path -LiteralPath $path -PathType Leaf) { |
| 21 |
$backup_path = "$path.$pid." + [DateTime]::Now.ToString("yyyyMMdd-HHmmss") + ".bak" |
| 22 |
Try { |
| 23 |
Copy-Item -LiteralPath $path -Destination $backup_path |
| 24 |
} |
| 25 |
Catch { |
| 26 |
throw "Failed to create backup file '$backup_path' from '$path'. ($($_.Exception.Message))" |
| 27 |
} |
| 28 |
} |
| 29 |
return $backup_path |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
# This line must stay at the bottom to ensure all defined module parts are exported |
| 34 |
Export-ModuleMember -Function Backup-File |