我想比较几个文件使用md5哈希与PowerShell 2.0。代码起作用,问题在于速度慢。 在第3步需要更长的时间。 大约需要比较500个文件。 你能否看到一种让它更快的方法,比如,不要每次都做第3步?powershell MD5比较太慢
write-host "1"
$COMP_ORI=$LOCAL_HOME+"\"+$PROG+"\"+$COMPARE
$file_ori = Get-ChildItem -Path $COMP_ORI -name
write-host "2"
$COMP_DEST="\\"+$HOSTIP[$i]+"\"+$PROG_PATH
$file_dest = Get-ChildItem -PATH $COMP_DEST -name
write-host "3"
for ($i=0; $i -lt $file_ori.Count; $i++) {
write-host "compare md5" $i
if (Get-ChildItem -PATH $COMP_DEST -name -Include $file_ori[$i]) {
$md5 = New-Object -TypeName system.Security.Cryptography.MD5CryptoServiceProvider
write-host "4"
$hash_ori = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($COMP_ORI+"\"+$file_ori[$i])))
write-host "5"
$hash_dest = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($COMP_DEST+"\"+$file_ori[$i])))
write-host "6"
if ($hash_ori -ne $hash_dest) {
$out=$file_ori[$i]
$out=$out+" IS DIFFERENT"
output message_solo $out
}
}
}
[codereview.se]是用于改进工作代码的stackexchange站点。你可以尝试在那里发布它,但计算md5sums是一个非常着名的算法,你不可能加快这一部分。 –
但是你可以通过调用一次'Get-ChildItem -PATH $ COMP_DEST [..]'来加速并存储结果,然后使用'-contains'来测试。列出远程文件共享500次不会很快。如果您可以让远程服务器使用PowerShell远程计算来计算dest文件的散列值,那么可以节省通过网络读取文件数据。 – TessellatingHeckler