Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $baseDirectory = "...\files"
- $fileDestination = "...\folder_of_hash"
- $hashFilePath = "...\hashes.txt"
- $hashesFromFile = @{}
- $currentHashes = @{}
- function mapAllFiles($basePath) {
- $subFolders = Get-ChildItem $basePath -Directory | foreach {$basePath + "\" + $_.Name} | foreach {mapAllFiles $_}
- $allFilesPath = Get-ChildItem $basePath -File | foreach {$basePath + "\" + $_.Name} | foreach {addFileToMap $_}
- }
- function exportToFile {
- $currentHashes.GetEnumerator() | Sort-Object Name | foreach {"{0} = {1}" -f $_.Name,$_.Value} | Add-Content "$fileDestination\hashes.txt"
- }
- function generateHashFile {
- $currentHashes = @{}
- exportToFile (mapAllFiles $baseDirectory)
- }
- function addFileToMap($filePath) {
- $hash = Get-FileHash $filePath -Algorithm SHA256 | Select Hash
- $currentHashes.Add($filePath.replace($baseDirectory, ""), $hash.Hash)
- }
- function compareHashes {
- $currentHashes = @{}
- $hashesFromFile = Get-Content $hashFilePath -Raw | ConvertFrom-StringData
- mapAllFiles $baseDirectory
- foreach ($file in $currentHashes.Keys) {
- if ($currentHashes[$file] -ne $hashesFromFile[$file]){
- Write-Host "Mismatch between hashes at : $file" -BackgroundColor "Red" -ForegroundColor "White"
- exit
- }
- else {
- Write-Host "$file matched ✓" -BackgroundColor "Green" -ForegroundColor "White"
- }
- }
- Write-Host "all files matched ✓✓✓" -BackgroundColor "Green" -ForegroundColor "Black"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement