I'm moving some files from a Linux machine to a Windows machine over a low-speed, possibly buggy experimental communications channel that I want to test. One of the tests is to transfer large numbers of large and small files and verify their cryptographic hashes at the receiving end. On the Linux side, we're using md5sum
to generate file hashes like so:
md5sum * > files.md5
Then the files are transmitted from the Linux machine to the Windows 10 machine. What I'd like to do next is to verify the hashes on the plain-vanilla Windows machine (no Cygwin installed). So to mimic the default operation of md5sum -c files.md5
which would go through, line by line and verify each md5 checksum, I've written this Powershell script. I'm a lot more at home in bash than in Powershell, so thought I might benefit from a review.
param (
[Parameter(Mandatory=$true)][string]$infile
)
$basedir = Split-Path -Parent $infile
$badcount = 0
foreach ($line in [System.IO.File]::ReadLines("$infile")) {
$sum, $file = $line.split(' ')
$fullfile = "$basedir\$file"
$filehash = Get-FileHash -Algorithm MD5 $fullfile
if ($sum -eq $filehash.Hash) {
Write-Host $file ": OK"
} else {
Write-Host $file ": FAILED"
$badcount++
}
}
if ($badcount -gt "0") {
Write-Host "WARNING:" $badcount "computed checksums did NOT match"
}