windowspowershellmd5nagiosget-filehash

Nagios plugin to check md5 directory sum in Windows powershell


Hi im trying to make my own plugin to check md5 sum inside directory and compare it with old one.

I wrote a script below

$Patch1= "C:\Users\User\Downloads\"
$Patch2= "D:\sqls\"
# Check both hashes are the same
Function Get-DirHash($Path1) {
    gci -File -Recurse $Path1 | Get-FileHash -Algorithm MD5 | select -ExpandProperty Hash | Out-File "C:/Program Files/temp.txt" -NoNewline
    $temp="C:/Program Files/temp.txt"
    $hash=Get-FileHash -Algorithm MD5 $temp
    $hash.Path=$Path1
    return $hash
}
Function Get-DirHash2($Path2) {
    gci -File -Recurse $Path2 | Get-FileHash -Algorithm MD5 | select -ExpandProperty Hash | Out-File "C:/Program Files/temp2.txt" -NoNewline
    $temp2="C:/Program Files/temp2.txt"
    $hash2=Get-FileHash -Algorithm MD5 $temp2
    $hash2.Path=$Path2
    return $hash2
}

Get-DirHash($Patch1).Hash
Get-DirHash2($Patch2).Hash
if (Get-DirHash($Patch1) -eq Get-DirHash2($Patch2)) {
    Write-Host 'Get-FileHash results are consistent' -ForegroundColor Green
} else {
    Write-Host 'Get-FileHash results are inconsistent!!' -ForegroundColor Red
}

But the output says that hashes are always equal

Algorithm       Hash                                                                   Path
---------       ----                                                                   ----
MD5             1BF506BB988C14CD8D1F04F239AE401C
MD5             1BF506BB988C14CD8D1F04F239AE401C
Get-FileHash results are consistent

Do you guys got any idea how to make this ?


Solution

  • You can use the same Get-DirHash for both paths.

    $Patch1 = "D:\test1"
    $Patch2 = "D:\test2"
    
    function Get-DirHash([string]$Path) {
        # create a new temporary file (returns a FileInfo object)
        $temp = New-TemporaryFile
        Get-ChildItem -File -Recurse $Path | 
            Get-FileHash -Algorithm MD5 | 
            Select-Object -ExpandProperty Hash | 
            Out-File -FilePath $temp.FullName -NoNewline -Encoding ascii
    
        $hash = $temp | Get-FileHash -Algorithm MD5 | 
                        Select-Object Algorithm,Hash, @{Name = 'Path'; Expression = {$Path}}
        $temp | Remove-Item 
        return $hash
    }
    
    # Check both hashes are the same
    
    $hash1 = Get-DirHash($Patch1)
    $hash2 = Get-DirHash($Patch2)
    
    if ($hash1.Hash -eq $hash2.Hash) {
        Write-Host 'Get-FileHash results are consistent' -ForegroundColor Green
    } else {
        Write-Host 'Get-FileHash results are inconsistent!!' -ForegroundColor Red
    }
    
    # output the objects on screen
    $hash1
    $hash2
    

    Using two different folders for $Patch1 and $Patch2 the result is

    Get-FileHash results are inconsistent!!
    
    Algorithm Hash                             Path    
    --------- ----                             ----    
    MD5       3C13B5C3F5D3EFC25BAE427CAC194F8D D:\test1
    MD5       DC21045CDDFF056E88933D82CB18DAEC D:\test2