我正在通过一个低速的,可能是错误的实验通信通道将一些文件从Linux机器转移到Windows机器上,我想测试这个通道。其中一个测试是在接收端传输大量大小文件并验证它们的加密散列。在Linux方面,我们使用md5sum生成文件散列,如下所示:
md5sum * > files.md5然后将文件从Linux机器传输到Windows 10机器。接下来我要做的是验证普通Windows机器上的散列(没有安装Cygwin )。因此,为了模拟md5sum -c files.md5的默认操作,它将逐行进行,并验证每个md5校验和,我编写了这个Powershell脚本。比起Powershell,我更多的是呆在bash,所以我想我可能会从评论中受益。
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"
}发布于 2020-07-22 20:33:48
以下是我要做的一些改变。这些想法..。
Get-Content而不是ReadLines(),除非您处理大量的文件,否则速度差异并不大。使用标准cmdlet,除非从其他方面获得有意义的好处。[PSCustomObject]以保存所需的结果项它所做的..。
#region/#endregion块。'__N/A__'。$Result集合@()中,并将.Count添加到末尾。密码..。
$SourceDir = $env:TEMP
$HashFileName = 'FileHashList.txt'
$FullHashFileName = Join-Path -Path $SourceDir -ChildPath $HashFileName
#region >>> make a hash list to compare with
# remove this entire "#region/#endregion" block when ready to work with your real data
$HashList = Get-ChildItem -LiteralPath $SourceDir -Filter '*.log' -File |
ForEach-Object {
'{0} {1}' -f $_.Name, (Get-FileHash -LiteralPath $_.FullName-Algorithm 'MD5').Hash
}
# munge the 1st two hash values
$HashList[0] = $HashList[0] -replace '.{5}输出..。FileName CopyOK OriHash CopyHash
-------- ------ ------- --------
Genre-List_2020-07-07.log False 7C0C605EA7561B7020CBDAE24D1--BAD 7C0C605EA7561B7020CBDAE24D140E40
Genre-List_2020-07-14.log False 20F234ACE66B860821CF8F8BD5E--BAD 20F234ACE66B860821CF8F8BD5EC144D, '--BAD'
$HashList[1] = $HashList[1] -replace '.{5}输出..。A47, '--BAD'
$HashList |
Set-Content -LiteralPath $FullHashFileName
#endregion >>> make a hash list to compare with
$Result = foreach ($Line in (Get-Content -LiteralPath $FullHashFileName))
{
$TestFileName, $Hash = $Line.Split(' ')
$FullTestFileName = Join-Path -Path $SourceDir -ChildPath $TestFileName
if (Test-Path -LiteralPath $FullTestFileName)
{
$THash = (Get-FileHash -LiteralPath $FullTestFileName -Algorithm 'MD5').Hash
}
else
{
$THash = '__N/A__'
}
[PSCustomObject]@{
FileName = $TestFileName
CopyOK = $THash -eq $Hash
OriHash = $Hash
CopyHash = $THash
}
}
$Result.Where({$_.CopyOK -eq $False})输出..。
A47
https://codereview.stackexchange.com/questions/245882
复制相似问题