我正在尝试在哈希级别比较两个文件夹。
我得到了哈希信息;算法,哈希,路径 $ SourceDocs和$ DestDocs都具有算法,哈希,路径
当我运行Compare-Object时,唯一传递的值是SideIndicator
我在没有哈希的情况下运行了此命令,但是名称相似但不同的文件会正常通过
cls
$Folder1 = "E:\Backups\Offsite"
$Folder2 = "E:\s3-drive\Backup\Compare"
$Folder3 = "E:\Backups\Temp_transfer"
Write-host
$time = Get-Date -Format HH:mm
Write-host $time
Write-host $Folder1
Write-host $Folder2
Write-host $Folder3
Write-host
Write-host
$time = Get-Date -Format HH:mm
Write-host $time
Write-host "Getting Directory Hash: " $Folder1
$SourceDocs = Get-ChildItem –Path $Folder1 | foreach {Get-FileHash –Path $_.FullName -Algorithm MD5}
Write-host
Write-host "Results for: " $Folder1
$SourceDocs
Write-host
Write-host
$time = Get-Date -Format HH:mm
Write-host $time
Write-host "Getting Directory Hash: " $Folder2
$DestDocs = Get-ChildItem –Path $Folder2 | foreach {Get-FileHash –Path $_.FullName -Algorithm MD5}
Write-host
Write-host
Write-host "Results for: " $Folder2
$DestDocs
Write-host
Write-host
$time = Get-Date -Format HH:mm
Write-host $time
Write-host "Comparing..."
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru
Write-host
$time = Get-Date -Format HH:mm
Write-host $time
Write-Host "These Files do not require action" -ForegroundColor Yellow
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru |
Where-Object { $_.SideIndicator -eq '==' } | Select-Object -Property path, hash, SideIndicator |
foreach-object -process{
Write-Host $_.path $_.hash $_.SideIndicator}
Write-host
$time = Get-Date -Format HH:mm
Write-Host "Duplicate Files to" -ForegroundColor Yellow -NoNewline; Write-Host " " $Folder1 -ForegroundColor Cyan
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru |
Where-Object { $_.SideIndicator -eq '<=' } | Select-Object -Property path, hash, SideIndicator |
foreach-object -process{
Write-Host $_.path $_.hash $_.SideIndicator}
Write-host
$time = Get-Date -Format HH:mm
Write-Host "Remove Files From" -ForegroundColor Yellow -NoNewline; Write-Host " " $Folder2 -ForegroundColor Cyan
Compare-Object -ReferenceObject $SourceDocs.Hash -DifferenceObject $DestDocs.Hash -IncludeEqual -Property path, hash, SideIndicator -PassThru |
Where-Object { $_.SideIndicator -eq '=>' } | Select-Object -Property path, hash, SideIndicator |
foreach-object -process{
Write-Host $_.path $_.hash $_.SideIndicator}
Break
这是输出
11:56
E:\Backups\Offsite
E:\s3-drive\Backup\Compare
E:\Backups\Temp_transfer
11:56
Getting Directory Hash: E:\Backups\Offsite
Results for: E:\Backups\Offsite
Algorithm Hash Path
--------- ---- ----
MD5 85D3D722185CC02750485DF0E0A7F1B4 E:\Backups\Offsite\Offsite_f...
MD5 41BFAAE4B1010D0FD09C669A4707ACF6 E:\Backups\Offsite\Offsite_f...
MD5 92641097960F47591B716282D61F7D3C E:\Backups\Offsite\Offsite_f...
MD5 B48374310B920AE3F4C3F26DBC7564B5 E:\Backups\Offsite\Offsite_f...
MD5 D41D8CD98F00B204E9800998ECF8427E E:\Backups\Offsite\Offsite_f...
MD5 D41D8CD98F00B204E9800998ECF8427E E:\Backups\Offsite\Offsite_f...
MD5 24DF814C247EDEED55DBF875B2BAE85B E:\Backups\Offsite\Offsite_f...
MD5 4BA69B5F53C40C48C3C032E883CC12EB E:\Backups\Offsite\Offsite_f...
12:14
Getting Directory Hash: E:\s3-drive\Backup\Compare
Results for: E:\s3-drive\Backup\Compare
MD5 85D3D722185CC02750485DF0E0A7F1B4 E:\s3-drive\Backup\Compare...
MD5 92641097960F47591B716282D61F7D3C E:\s3-drive\Backup\Compare...
MD5 B48374310B920AE3F4C3F26DBC7564B5 E:\s3-drive\Backup\Compare...
MD5 24DF814C247EDEED55DBF875B2BAE85B E:\s3-drive\Backup\Compare...
MD5 D41D8CD98F00B204E9800998ECF8427E E:\s3-drive\Backup\Compare...
MD5 0627B4727E2BFE1D1CB7F06B82BFCC5C E:\s3-drive\Backup\Compare...
MD5 4BA69B5F53C40C48C3C032E883CC12EB E:\s3-drive\Backup\Compare...
12:27
Comparing...
85D3D722185CC02750485DF0E0A7F1B4
41BFAAE4B1010D0FD09C669A4707ACF6
92641097960F47591B716282D61F7D3C
B48374310B920AE3F4C3F26DBC7564B5
D41D8CD98F00B204E9800998ECF8427E
D41D8CD98F00B204E9800998ECF8427E
24DF814C247EDEED55DBF875B2BAE85B
4BA69B5F53C40C48C3C032E883CC12EB
12:27
These Files do not require action
==
==
==
==
==
==
==
Copy Files to E:\Backups\Offsite
<=
Remove Files From E:\s3-drive\Backup\Compare
=>
这里的整个想法是比较两个文件夹并采用以下三个选项之一:
任何见识都会受到赞赏
更新...
Get-FileHash现在看起来像这样:
$SourceDocs = Get-ChildItem -path $Folder1 -File | foreach {
Get-FileHash –Path $_.FullName -Algorithm MD5
New-Object PSCustomObject -Property @{
"DisplayName" = (Split-Path $_.FullName -Leaf -Resolve)}}
由于某种原因,我无法仅显示名称,所以我使用了New-Object并强制使用
和compare语句也必须进行调整:
$compare = Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property Hash -IncludeEqual -PassThru | Select-Object Hash, SideIndicator, Path
到
$compare = Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property Hash -IncludeEqual -PassThru | Select-Object Hash, SideIndicator, DisplayName
答案 0 :(得分:0)
更新
从您正在比较的部分来看,我认为这将为您提供所需的输出:
Write-host "Comparing..."
$compare = Compare-Object -ReferenceObject $SourceDocs -DifferenceObject $DestDocs -Property Hash -IncludeEqual -PassThru | Select-Object Hash, SideIndicator, Path
Write-host
$doNothing = $compare | Where-Object { $_.SideIndicator -eq '==' }
if ($doNothing) {
Write-Host "These Files do not require action" -ForegroundColor Yellow
$doNothing | Format-Table -AutoSize
}
$doCopy = $compare | Where-Object { $_.SideIndicator -eq '<=' }
if ($doCopy) {
Write-Host "Copy these files" -ForegroundColor Cyan
$doCopy | Format-Table -AutoSize
}
$doDelete = $compare | Where-Object { $_.SideIndicator -eq '=>' }
if ($doDelete) {
Write-Host "Delete these files" -ForegroundColor Magenta
$doDelete | Format-Table -AutoSize
}
并回答标题中的问题:在Compare-Object语句中,将其设置为仅比较Hash
属性,在此处应比较完整对象$SourceDocs
和$DestDocs