From ca5bb08c071e37be9613ce5df2bdfd6a9a2f8563 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 20 Aug 2016 00:22:33 +0200 Subject: [PATCH] Add files via upload Converted python script to powershell for machines where is not powershell installed --- Tools/Verification/DuplicatiVerify.ps1 | 94 ++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Tools/Verification/DuplicatiVerify.ps1 diff --git a/Tools/Verification/DuplicatiVerify.ps1 b/Tools/Verification/DuplicatiVerify.ps1 new file mode 100644 index 000000000..08f52c47d --- /dev/null +++ b/Tools/Verification/DuplicatiVerify.ps1 @@ -0,0 +1,94 @@ +Set-StrictMode -Version 5 + +function HexToBase64 +{ + param + ( + [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] + [string]$s + ) + [array]$return = @() + + for ($i = 0; $i -lt $s.Length ; $i += 2) { + $return += [Byte]::Parse($s.Substring($i, 2), [System.Globalization.NumberStyles]::HexNumber) + } + + Write-Output $([Convert]::ToBase64String($return)) +} + +function Verify-Hashes +{ + [CmdletBinding(PositionalBinding=$false)] + param + ( + [Parameter(Mandatory=$true)] + [ValidateNotNullOrEmpty()] + [string]$filename + ) + + [int]$errorCount = 0 + [int]$checked = 0 + + if(-not $(Test-Path $filename)) { + Write-Host "Specified file does not exist: $filename" + return -1 + } + + [string]$folder = Split-Path -Path $filename + + $remoteVolumes = (Get-Content $filename) -Join "`n" | ConvertFrom-Json + + foreach ($remoteVolume in $remoteVolumes) { + [string]$volFileName = $remoteVolume.Name + [long]$volFileSize = $remoteVolume.Size + [string]$volFilePath = Join-Path -Path $folder -ChildPath $volFileName + + if(-not $(Test-Path $volFilePath)) { + Write-Host "File missing: $volFilePath" -ForegroundColor Red + $errorCount++ + } else { + $checked++ + + Write-Host "Verifying file $volFileName" + + $shaHash = Get-FileHash -Path $volFilePath -Algorithm SHA256 + + if($remoteVolume.Hash -ne $(HexToBase64 $shaHash.Hash)) { + Write-Host "*** Hash check failed for file: $volFileName" -ForegroundColor Red + $errorCount++ + } + } + } + + if($errorCount -gt 0) { + Write-Host "Errors were found" -ForegroundColor Red + } else { + Write-Host "No errors found" -ForegroundColor Green + } + + return $errorCount +} + +[string]$argument = "" +if ($args.Length -eq 1) { $argument = $args[0] } +else { $argument = $PSScriptRoot } + + +if(-not $(Test-Path $argument)) { + Write-Host "No such file or directory: $argument" -ForegroundColor Red +} + +if(Test-Path $argument -PathType Leaf) { + Verify-Hashes -filename $argument +} else { + [int] $files = 0 + + Get-ChildItem $argument -Filter "*-verification.json" | + Foreach-Object { + $files++ + Write-Host "Verifying file: $_.Name" + Verify-Hashes -filename $_.FullName + } + + if ($files -eq 0) { Write-Host "No verification files in folder: $argument" } +} \ No newline at end of file