Skip to content

Instantly share code, notes, and snippets.

@DJStompZone
Created April 28, 2024 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DJStompZone/88bf057fd5fcd0f57f01e213cfcf695a to your computer and use it in GitHub Desktop.
Save DJStompZone/88bf057fd5fcd0f57f01e213cfcf695a to your computer and use it in GitHub Desktop.
Copy files recursively from the source to the target, verifying the files' hashes to ensure integrity
function CopyAndVerify {
param (
[Parameter(Mandatory=$true)]
[string]$sourceDir,
[Parameter(Mandatory=$true)]
[string]$destinationDir,
[switch]$zip
)
<#
.SYNOPSIS
Copies files or directories from a source to a destination and verifies integrity.
.DESCRIPTION
The CopyAndVerify function copies files from a specified source directory to a specified destination.
It verifies the integrity of each file using MD5 hash comparison.
When the -zip switch is used, it compresses the source into a zip file, copies it, and verifies the integrity of the zip file.
.PARAMETER sourceDir
Specifies the path to the source directory whose contents are to be copied.
.PARAMETER destinationDir
Specifies the path to the destination directory where the contents are to be copied to.
.PARAMETER zip
When used, the source directory is compressed into a zip file before being copied.
The integrity of the zip file is verified after copying.
.EXAMPLE
CopyAndVerify -sourceDir "C:\source" -destinationDir "D:\destination"
Copies all files from C:\source to D:\destination and verifies each file's integrity.
.EXAMPLE
CopyAndVerify -sourceDir "C:\source" -destinationDir "D:\destination" -zip
Compresses C:\source into a zip file, copies it to D:\destination, and verifies the zip file's integrity.
.INPUTS
None. You cannot pipe objects to CopyAndVerify.
.OUTPUTS
Output to console. Displays verification status or errors for each file.
.NOTES
The function can handle a large number of files and deep directory structures.
The -zip parameter is useful for reducing transfer times and simplifying verification of many small files.
.LINK
Get-FileHash
#>
if ($zip) {
$zipFile = "$sourceDir.zip"
Compress-Archive -Path $sourceDir -DestinationPath $zipFile -Force
Copy-Item -Path $zipFile -Destination $destinationDir -Force
$sourceHash = Get-FileHash -Path $zipFile -Algorithm MD5
$destinationZipFile = Join-Path $destinationDir (Split-Path -Leaf $zipFile)
$destinationHash = Get-FileHash -Path $destinationZipFile -Algorithm MD5
if ($sourceHash.Hash -eq $destinationHash.Hash) {
Write-Host "Verified zip: $zipFile"
Expand-Archive -Path $destinationZipFile -DestinationPath $destinationDir -Force
Remove-Item -Path $zipFile -Force
Remove-Item -Path $destinationZipFile -Force
} else {
Write-Host "Hash mismatch for zip: $zipFile" -ForegroundColor Red
}
} else {
if (-not (Test-Path -Path $destinationDir)) {
New-Item -ItemType Directory -Force -Path $destinationDir
}
$files = Get-ChildItem -Path $sourceDir -Recurse -File
foreach ($file in $files) {
try {
$destinationPath = $file.FullName.Replace($sourceDir, $destinationDir)
$destinationDirPath = [System.IO.Path]::GetDirectoryName($destinationPath)
if (-not (Test-Path -Path $destinationDirPath)) {
New-Item -ItemType Directory -Force -Path $destinationDirPath
}
Copy-Item -Path $file.FullName -Destination $destinationPath -Force
$sourceHash = Get-FileHash -Path $file.FullName -Algorithm MD5
$destinationHash = Get-FileHash -Path $destinationPath -Algorithm MD5
if ($sourceHash.Hash -eq $destinationHash.Hash) {
Write-Host "Verified: $($file.Name)"
} else {
Write-Host "Hash mismatch for: $($file.Name)" -ForegroundColor Red
}
} catch {
Write-Host "Error copying file: $($file.Name). Error details: $_" -ForegroundColor Red
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment