【发布时间】:2021-04-05 01:42:54
【问题描述】:
我对 Powershell 很陌生,所以请原谅我缺乏经验或缺乏最佳实践。 我正在编写一个脚本,它将首先以 CSV 格式存储两个不同目录的 MD5 哈希值。然后我想比较这两个文件,如果这两个 CSV 文件有任何更改(即文件的 MD5 哈希不匹配,或者源文件夹的 CSV 文件中的一个文件在目标文件夹的 CSV 文件中不存在),我想生成一个新列表,其中包含不匹配或缺失文件的详细信息。
除此之外,我想知道是否有更快的方法来比较这两个文件,因为我正在处理的文件夹非常大,其中包含大约 12k 个文件。我在想,也许我会尝试保持源 CSV 完整,并在检查和验证时从目标 CSV 中一一删除条目。谁能帮我解决这个问题?
#Getting the MD5 hash of the Installer and storing it a csv format
$SourcePath = Get-ChildItem -Path C:\source -Recurse
$SourcerHash = foreach ($File in $SourcePath)
{
Get-FileHash $File.FullName -Algorithm MD5
}
$SourceHash | Export-Csv -Path C:\Users\abcd\Desktop\CSVExports\SourceHash.csv
#Getting the MD5 hash of the destination directory and storing it in a csv format
$DestinationPath = Get-ChildItem -Path C:\destination -Recurse
$DestinationHash = foreach ($File in $DestinationPath)
{
Get-FileHash $File.FullName -Algorithm MD5
}
$DestinationHash | Export-Csv -Path C:\Users\abcd\Desktop\CSVExports\DestinationHash.csv
#Comparing the hashes of Installer and Destination directories
Compare-Object -ReferenceObject (Import-Csv C:\Users\abcd\Desktop\CSVExports\InstallerHash.csv) -DifferenceObject (Import-Csv C:\Users\abcd\Desktop\CSVExports\DestinationHash.csv) -Property Hash | Export-Csv C:\Users\abcd\Desktop\CSVExports\ResultTable
【问题讨论】:
-
"...是比较两个文件的更快方法...",请参阅:Powershell Speed: How to speed up ForEach-Object MD5/hash check
-
您在 first 之后使用了错误的变量名。 InstallerHash 而不是 SourceHash。
标签: powershell csv hash md5