2016-02-29 70 views
0

我們有一個~4gb的有效負載,我們需要將它們複製到多個地理位置,並且某些時候程序集(dll文件)會被損壞。我使用獲得哈希函數在PowerShell和我的示例代碼片段看起來是這樣的如何在複製後對源和目標執行MD5驗證

# PowerShell Invoke-Command source Machine has check 
Clear-Host 
Invoke-Command -ScriptBlock {dir C:\swtools\7-Zip -Recurse | Where-Object {!$_.psiscontainer } | get-hash | Out-File c:\temp\source.md5.txt} 

# PowerShell Invoke-Command on Remote Computer to check filehash at Dest 
Clear-Host 
Invoke-Command -Computer Remote_Host -ScriptBlock {dir e:\downloads\7-zip -Recurse | Where-Object {!$_.psiscontainer } | get-hash } | Out-File c:\temp\dest.md5.txt 

# PowerShell compare to check if the md5 hashes are equal at source and dest 

Compare-Object $(Get-Content c:\temp\source.md5.txt) $(Get-Content c:\temp\dest.md5.txt) -includeequal 

這並不因爲Get-哈希的輸出包含文件路徑添加到文件名上解決問題,所以我真的不能與文件名和哈希值進行1:1比較,源文件名和散列值來自目的地,因爲輸出包含完整文件路徑,因此輸出文件路徑不同

那麼,確保從源複製到所有程序集的md5校驗和的最佳方法是什麼?目的地是否匹配?

注:我沒有看創建額外的文件,我只推動文件名和散列通過正則表達式等,時間和內存將是一個問題。

+0

爲什麼不能通過文件先運行腳本並修改文件名?當然,你有一個算法將源代碼轉換爲目標路徑? –

回答

1

你可以管,以便Tee-Object轉儲到文件(如果你覺得有必要轉儲到文件),然後Select-Object修改Path值排除的根路徑,並捕獲在一個變量來運行您的Compare-Object針對。以下內容將輸出您的文件,並捕獲Hash輸出的修改版本,其中Path將排除根路徑,並僅包含子文件夾和文件名。

# PowerShell Invoke-Command source Machine has check 
Clear-Host 
$LocalRoot = [RegEx]::Escape('C:\swtools\7-Zip') 
$LocalHash = Invoke-Command -ScriptBlock {dir C:\swtools\7-Zip -Recurse | Where-Object {!$_.psiscontainer } | get-hash} | Tee-Object -File c:\temp\source.md5.txt | Select *,@{l='Path';e={$_.Path -replace $LocalRoot}} -ExcludeProperty Path 

# PowerShell Invoke-Command on Remote Computer to check filehash at Dest 
Clear-Host 
$DestRoot = [RegEx]::Escape('e:\downloads\7-zip') 
$DestHash = Invoke-Command -Computer Remote_Host -ScriptBlock {dir e:\downloads\7-zip -Recurse | Where-Object {!$_.psiscontainer } | get-hash } | Tee-Object -file c:\temp\dest.md5.txt | Select *,@{l='Path';e={$_.Path -replace $DestRoot}} -ExcludeProperty Path 

# PowerShell compare to check if the md5 hashes are equal at source and dest 

Compare-Object $LocalHash $DestHash -includeequal 
+0

它在500個文件的列表上工作,將需要在20000個文件上運行它並查看它是如何運行的,謝謝! – holyAsh