-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_file_hashes.ps1
64 lines (55 loc) · 1.82 KB
/
check_file_hashes.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$intOK = 0;
$intWarning = 1;
$intCritical = 2;
$intUnknown = 3;
$errcode = $intUnknown;
$output = "";
$failtrigger = $false
$app1PathToBinary = '\\srv1.contoso.com\WebServices\'
$app2PathToBinary = '\\srv2.contoso.com\WebServices\'
function getmd5hash ($pathToFile) {
$md5 = New-Object System.Security.Cryptography.MD5CryptoServiceProvider
$fileToHash = [System.IO.File]::Open($pathToFile,'Open','Read','ReadWrite');
$hashOfFile = [String]::Join("", ($md5.ComputeHash($fileToHash) | % {$_.ToString("x2")}))
$fileToHash.Dispose()
return [string]$hashOfFile
}
try
{
$ErrorActionPreference = "Stop";
if ((Test-Path $app1PathToBinary) -and (Test-Path $app2PathToBinary)) {
$arrayOfApp1Binary = Get-ChildItem $app1PathToBinary | where {$_.Name -match ".+\.dll$" -or $_.Name -match ".+\.exe$"}
$arrayOfApp2Binary = Get-ChildItem $app2PathToBinary | where {$_.Name -match ".+\.dll$" -or $_.Name -match ".+\.exe$"}
foreach ($binFile in $arrayOfApp1Binary) {
$binFile2 = $arrayOfApp2Binary | where {$_.Name -eq $binFile.Name}
if (!$binFile2) {
$failtrigger = $true
$output += "$($binFile.Name) - no corresponding file; "
} else {
$hash1 = getmd5hash $binFile.FullName
$hash2 = getmd5hash $binFile2.FullName
if ($hash1 -eq $hash2) {
$output += "$($binFile.Name) - OK; "
} else {
$failtrigger = $true
$output += "$($binFile.Name) - hashes did not match; "
}
}
}
if ($failtrigger) {
$errcode = $intCritical
} else {
$errcode = $intOK
}
} else {
$errcode = $intCritical
$output = 'Could not access application folder!'
}
}
catch
{
$errcode = $intCritical
$output = $_.Exception.Message;
}
write-host $output;
exit $errcode;