希望这是一个简单的问题,是否存在等效于 Windows 的简单命令行md5sum --check [files.md5]
?或者,我可以将其编写为批处理文件。
我可以很好地生成哈希文件,但这是目标处的检查,与 md5 文件进行比较,这很棘手。如果可以在批处理文件而不是 PowerShell 中运行,则可以获得加分(需要尝试创建一些相对简单的程序供用户定期运行)。
非常感谢!
答案1
Powershell 是你的好朋友。你可以使用 CMD/BAT 包装器来运行它,这样就可以运行脚本了
clear-host
$sourcemd5 = get-content C:\temp\cp053854.md5
cd C:\Temp
foreach ($datarow in $sourcemd5){
# try this to replace mutli spaces with single. uses regex
$datarow = $($datarow -replace '\s+', ' ')
$dlhash = $null
$currentFileHash = $null
$currentFileName = $null
#use the first (0) token found (hash value)
$currentFileHash = ($datarow.Split(" "))[0]
#use the second (1) token found (filename value)
$currentFileName = ($datarow.Split(" "))[1]
#$currentFileName check if has leading asterisk*.
if ($currentFileName -match "\*"){
#remove it
$currentFileName = $currentFileName.Trim("\*")
}
# Generate an MD5 of the filesystem file
$dlhash = get-filehash $currentFileName -Algorithm MD5
#set the generated hash value as string for comparison to the text file.
[string]$dlhashstr = $dlhash.Hash
write-output "checking file [$currentfileName]"
write-output "downloaded file hash [$dlhashstr]"
write-output "hash value should be [$currentFileHash]"
#perform a case insensitive comparison if MD5file uses caps or not.
if ($dlhashstr -eq $currentFileHash){
write-output "File hashes correctly match up [$currentFileName]"
}else{
Write-Warning "MD5 checksum mismatch [$currentFileName]"
}
}