使用Get-FileHash

时间:2017-03-06 09:56:53

标签: powershell

我有一个脚本,用文件哈希比较两个图像:

if((Get-FileHash C:\Users\UserName\Desktop\OrImg).hash -ne (Get-FileHash C:\Users\UserName\Desktop\RefImg).hash)
{"Files are different"}
else {"Files are the same"}

但是我如何通过哈希属性确定两个图像是不同的?出于测试目的,我创建了两个不同的图像(大小,内容)但是当我比较它们时 - 哈希是相同的。也许我做错了什么?

5 个答案:

答案 0 :(得分:2)

这将返回具有不同文件哈希的所有文件。注意:这不会检查文件是否存在于目录2中,也不会检查目录2是否包含目录2中没有的文件。

$leftDir = "D:\tmp\1"
$rightDir = "D:\tmp\2"

$differentFiles = Get-ChildItem $leftDir | Where-Object {
    ($_ | Get-FileHash).Hash -ne (Get-FileHash (Join-Path $rightDir $_.Name)).Hash
} 

if (-not $differentFiles)
{
    'All files are the same'
}
else
{
    $differentFiles | Foreach-Object {Write-Output "File $($_.Name) is different"}
}

答案 1 :(得分:1)

我使用md5 sum为我的测试脚本做了这个Windows PowerShell。它对我来说很好。

    $FilePathSource = "C:\Susantha\Test.txt"
    $FilePathDestination = "C:\Susantha\Test.txt"

    try
    {
    $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    $hashedFileSource = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePathSource)))
    $hashedFileDestination = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($FilePathDestination)))

    [bool]$Result = $hashedFileSource.CompareTo($hashedFileDestination)

      if ($Result) {
       Write-Host "Not the Same Files :("
       }
      else{
       Write-Host "Same Files :)"
       }
   }
   catch
   {
   Write-Host  $_.Exception.Message
   Write-Host $_.Exception.ItemName
   Break
   }

答案 2 :(得分:0)

试试这个

$x = (Get-FileHash 'C:\temp\1.bmp').hash
$z = (Get-FileHash 'C:\temp\2.bmp').hash
[bool]$w = $z.CompareTo($x)
if ($w) {
Write-Host "Not the same"
}

答案 3 :(得分:0)

我问了一个类似的问题,关于我编写的脚本占了一半。这是完成的文章。

此处参考Compare directories and sub directories and replace files based on MD5 hash

感谢@LotPings的帮助

$Source=  "D:\Folder1"
$Destination = "D:\folder2"

get-childitem $Source -Recurse | foreach {
#Calculate hash using Algorithm MD5 reference http://www.tomsitpro.com/articles/powershell-file-hash-check,2-880.html
$SrcFile = $_.FullName
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5

$DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination
Write-Host "Copying $SrcFile to $DestFile" -ForegroundColor Yellow

if (Test-Path $DestFile) {
    #Check the hash sum of the file copied
    $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5

    #compare them up
    if ($SrcHash.hash -ne $DestHash.hash) {
        Write-Warning "$SrcFile and $DestFile Files don't match!" 
        Copy-Item $SrcFile -Destination $DestFile -Force
    } else {
        Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor Green
    }
} else {
    Copy-Item $SrcFile -Destination $DestFile -Force
}
} 

答案 4 :(得分:0)

我做了一个小小的改变。不确定为什么你有2个x else语句。复制已关闭两次。 下面的更改将显示RED中的应对。

$Source =  "folder1"
$Destination = "folder2"

get-childitem $Source -Recurse | foreach {
#Calculate hash using Algorithm MD5 reference http://www.tomsitpro.com/articles/powershell-file-hash-check,2-880.html
$SrcFile = $_.FullName
$SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5

$DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination
Write-Host
Write-Host "Comparing $SrcFile to $DestFile" -ForegroundColor Yellow

if (Test-Path $DestFile) {
    #Check the hash sum of the file copied
    $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5

    #compare them up
    if ($SrcHash.hash -ne $DestHash.hash) {
        Write-Warning "$SrcFile and $DestFile Files don't match!" 
            Write-Host "Copying $SrcFile to $DestFile" -ForegroundColor Red
        Copy-Item $SrcFile -Destination $DestFile -Force
    } else {
        Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor Green
    }

} 
} 
相关问题