Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. $baseDirectory = "...\files"
  2. $fileDestination = "...\folder_of_hash"
  3. $hashFilePath = "...\hashes.txt"
  4. $hashesFromFile = @{}
  5. $currentHashes = @{}
  6.  
  7. function mapAllFiles($basePath) {
  8. $subFolders = Get-ChildItem $basePath -Directory | foreach {$basePath + "\" + $_.Name} | foreach {mapAllFiles $_}
  9. $allFilesPath = Get-ChildItem $basePath -File | foreach {$basePath + "\" + $_.Name} | foreach {addFileToMap $_}
  10. }
  11.  
  12. function exportToFile {
  13. $currentHashes.GetEnumerator() | Sort-Object Name | foreach {"{0} = {1}" -f $_.Name,$_.Value} | Add-Content "$fileDestination\hashes.txt"
  14. }
  15.  
  16. function generateHashFile {
  17. $currentHashes = @{}
  18. exportToFile (mapAllFiles $baseDirectory)
  19. }
  20.  
  21. function addFileToMap($filePath) {
  22. $hash = Get-FileHash $filePath -Algorithm SHA256 | Select Hash
  23. $currentHashes.Add($filePath.replace($baseDirectory, ""), $hash.Hash)
  24. }
  25.  
  26. function compareHashes {
  27. $currentHashes = @{}
  28. $hashesFromFile = Get-Content $hashFilePath -Raw | ConvertFrom-StringData
  29.  
  30. mapAllFiles $baseDirectory
  31.  
  32. foreach ($file in $currentHashes.Keys) {
  33. if ($currentHashes[$file] -ne $hashesFromFile[$file]){
  34. Write-Host "Mismatch between hashes at : $file" -BackgroundColor "Red" -ForegroundColor "White"
  35. exit
  36. }
  37. else {
  38. Write-Host "$file matched ✓" -BackgroundColor "Green" -ForegroundColor "White"
  39. }
  40. }
  41.  
  42. Write-Host "all files matched ✓✓✓" -BackgroundColor "Green" -ForegroundColor "Black"
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement