Skip to content

Instantly share code, notes, and snippets.

@Aciid
Created February 10, 2024 22:37
Show Gist options
  • Save Aciid/cdc1d342f13e741504f04b1d5e9f6c74 to your computer and use it in GitHub Desktop.
Save Aciid/cdc1d342f13e741504f04b1d5e9f6c74 to your computer and use it in GitHub Desktop.
Acronis Trueimage - Remove hash from recovered files
# .\rename.ps1 -Operation DryRun -Path "D:\"
# .\rename.ps1 -Operation Rename -Path "D:\"
# Logs to D:\file_operations.log
param (
[Parameter(Mandatory=$true)]
[ValidateSet("DryRun", "Rename")]
[string]$Operation,
[Parameter(Mandatory=$true)]
[string]$Path
)
function Get-FileHashMD5 {
param (
[string]$FilePath
)
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$fileStream = [System.IO.File]::OpenRead($FilePath)
try {
$hash = [System.BitConverter]::ToString($md5.ComputeHash($fileStream)).Replace("-", "").ToLower()
}
finally {
$fileStream.Close()
$fileStream.Dispose()
}
return $hash
}
function Log-FileAction {
param (
[string]$FilePath,
[string]$ActionType # "DryRun" or "Rename"
)
$md5 = Get-FileHashMD5 -FilePath $FilePath
$size = (Get-Item $FilePath).length
$logMessage = "$FilePath, $md5, $size bytes"
# Specify the log file path; adjust as necessary.
$logFilePath = "D:\file_operations.log"
Add-Content -Path $logFilePath -Value $logMessage
if ($ActionType -eq "Rename") {
# Perform rename operation for "-Rename"
$newFilePath = $FilePath -replace '~[A-Z0-9]+$'
Rename-Item -Path $FilePath -NewName $newFilePath
}
}
function Invoke-Operation {
param (
[string]$Path,
[string]$ActionType
)
Get-ChildItem -Path $Path -Recurse | Where-Object { $_.Name -match '~[A-Z0-9]+$' } | ForEach-Object {
Log-FileAction -FilePath $_.FullName -ActionType $ActionType
}
}
# Main script execution based on the parameters provided
switch ($Operation) {
"DryRun" { Invoke-Operation -Path $Path -ActionType "DryRun" }
"Rename" { Invoke-Operation -Path $Path -ActionType "Rename" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment