在 Windows powershell 中检查 md5 目录总和的 Nagios 插件
Nagios plugin to check md5 directory sum in Windows powershell
嗨,我正在尝试制作我自己的插件来检查目录内的 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
你们知道怎么做吗?
您可以对两个路径使用相同的 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
嗨,我正在尝试制作我自己的插件来检查目录内的 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
你们知道怎么做吗?
您可以对两个路径使用相同的 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