【问题标题】:Nagios plugin to check md5 directory sum in Windows powershellNagios 插件在 Windows PowerShell 中检查 md5 目录总和
【发布时间】:2019-09-30 10:54:16
【问题描述】:

嗨,我正在尝试制作自己的插件来检查目录中的 md5 总和并将其与旧的进行比较。

我在下面写了一个脚本

$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
}

但是输出表明哈希值总是相等的

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

你们知道怎么做吗?

【问题讨论】:

    标签: windows powershell md5 nagios get-filehash


    【解决方案1】:

    您可以对两条路径使用相同的Get-DirHash

    $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
    

    $Patch1$Patch2 使用两个不同的文件夹,结果是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 2013-08-26
      • 2015-12-15
      • 1970-01-01
      相关资源
      最近更新 更多