Nagios插件在Windows Powershell中检查md5目录总和

时间:2019-05-13 13:07:54

标签: windows powershell md5 nagios get-filehash

您好,我正在尝试制作自己的插件来检查目录中的md5 sum并将其与旧版本进行比较。

我在下面写了一个脚本

$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

你们知道如何做吗?

1 个答案:

答案 0 :(得分:0)

两个路径都可以使用相同的layoutSubviews

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