waypoit debuguing
This commit is contained in:
@@ -3,11 +3,12 @@ param(
|
||||
# File(s) or wildcard pattern to compute checksum for
|
||||
[Parameter(Position = 0, ParameterSetName = 'Generate', ValueFromPipeline = $true)]
|
||||
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
|
||||
[Parameter(Position = 0, ParameterSetName = 'PrincessPi', ValueFromPipeline = $false)]
|
||||
[string[]]$FilePath,
|
||||
|
||||
# Custom output file (-f / -OutFile / -of)
|
||||
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
|
||||
[Alias('f', 'of', 'OF')]
|
||||
[Alias('f', 'of')]
|
||||
[string]$OutFile,
|
||||
|
||||
# Auto output file switch (-s / -Save)
|
||||
@@ -37,7 +38,7 @@ param(
|
||||
[switch]$Recursive,
|
||||
|
||||
# Princess Pi art flag (-pp / -PrincessPi)
|
||||
[Parameter(Mandatory = $false)]
|
||||
[Parameter(Mandatory = $true, ParameterSetName = 'PrincessPi')]
|
||||
[Alias('pp')]
|
||||
[switch]$PrincessPi,
|
||||
|
||||
@@ -197,6 +198,35 @@ EXAMPLES
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
function Invoke-ParallelHashing {
|
||||
param([array]$Items, [string]$Algorithm);
|
||||
|
||||
$total = $Items.Count
|
||||
$completed = 0
|
||||
$syncHash = [System.Collections.Hashtable]::Synchronized(@{ Count = 0 })
|
||||
|
||||
$Items | ForEach-Object -Parallel {
|
||||
$item = $_
|
||||
$algo = $using:Algorithm
|
||||
|
||||
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
|
||||
|
||||
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||
if (-not $relativePath) { $relativePath = $item.Name; };
|
||||
if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
|
||||
$relativePath = $relativePath.Substring(2);
|
||||
};
|
||||
|
||||
[PSCustomObject]@{
|
||||
Item = $item
|
||||
Hash = $hash
|
||||
RelativePath = $relativePath
|
||||
OutputLine = "${hash} ${relativePath}"
|
||||
ItemOutFile = "$($item.FullName).$($algo.ToLower())"
|
||||
}
|
||||
} -ThrottleLimit 16
|
||||
};
|
||||
}
|
||||
|
||||
process {
|
||||
@@ -281,8 +311,10 @@ process {
|
||||
};
|
||||
|
||||
$expectedHash = $Match.Trim().ToLower();
|
||||
$totalFiles = $allItems.Count;
|
||||
$currentIndex = 0;
|
||||
|
||||
$allItems | ForEach-Object -Parallel {
|
||||
$results = $allItems | ForEach-Object -Parallel {
|
||||
$item = $_
|
||||
$algo = $using:Algorithm
|
||||
$exp = $using:expectedHash
|
||||
@@ -296,15 +328,21 @@ process {
|
||||
TargetFile = $targetFile
|
||||
IsMatch = ($actualHash -eq $exp)
|
||||
}
|
||||
} -ThrottleLimit 16 | ForEach-Object {
|
||||
if ($_.IsMatch) {
|
||||
Write-Host "$($_.TargetFile): " -NoNewline;
|
||||
} -ThrottleLimit 16;
|
||||
|
||||
foreach ($res in $results) {
|
||||
$currentIndex++;
|
||||
Write-Progress -Activity "Matching Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||
|
||||
if ($res.IsMatch) {
|
||||
Write-Host "$($res.TargetFile): " -NoNewline;
|
||||
Write-Host "OK" -ForegroundColor Green;
|
||||
} else {
|
||||
Write-Host "$($_.TargetFile): " -NoNewline;
|
||||
Write-Host "$($res.TargetFile): " -NoNewline;
|
||||
Write-Host "FAILED" -ForegroundColor Red;
|
||||
};
|
||||
};
|
||||
Write-Progress -Activity "Matching Checksums ($Algorithm)" -Completed;
|
||||
}
|
||||
# --- VERIFICATION MODE (-c / -Check) ---
|
||||
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
|
||||
@@ -413,16 +451,19 @@ process {
|
||||
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())";
|
||||
};
|
||||
|
||||
$linesToWrite = $filesInDir | ForEach-Object -Parallel {
|
||||
$item = $_
|
||||
$algo = $using:Algorithm
|
||||
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
|
||||
"${hash} $($item.Name)"
|
||||
} -ThrottleLimit 16;
|
||||
$totalFiles = $filesInDir.Count;
|
||||
$currentIndex = 0;
|
||||
$linesToWrite = @();
|
||||
|
||||
foreach ($outputLine in $linesToWrite) {
|
||||
Write-Output $outputLine;
|
||||
$parallelResults = Invoke-ParallelHashing -Items $filesInDir -Algorithm $Algorithm;
|
||||
foreach ($res in $parallelResults) {
|
||||
$currentIndex++;
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Directory: $(Split-Path $dirPath -Leaf) [$currentIndex/$totalFiles]" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||
|
||||
$linesToWrite += $res.OutputLine;
|
||||
Write-Output $res.OutputLine;
|
||||
}
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||
|
||||
Set-Content -Path ($currentOutFile) -Value $linesToWrite -Encoding utf8;
|
||||
};
|
||||
@@ -436,70 +477,52 @@ process {
|
||||
$singleOutFile = "checksums.$($Algorithm.ToLower())";
|
||||
};
|
||||
|
||||
$linesToWrite = $allItems | ForEach-Object -Parallel {
|
||||
$item = $_
|
||||
$algo = $using:Algorithm
|
||||
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
|
||||
|
||||
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||
if (-not $relativePath) { $relativePath = $item.Name; };
|
||||
if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
|
||||
$relativePath = $relativePath.Substring(2);
|
||||
};
|
||||
"${hash} ${relativePath}"
|
||||
} -ThrottleLimit 16;
|
||||
$totalFiles = $allItems.Count;
|
||||
$currentIndex = 0;
|
||||
$linesToWrite = @();
|
||||
|
||||
foreach ($outputLine in $linesToWrite) {
|
||||
Write-Output $outputLine;
|
||||
$parallelResults = Invoke-ParallelHashing -Items $allItems -Algorithm $Algorithm;
|
||||
foreach ($res in $parallelResults) {
|
||||
$currentIndex++;
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||
|
||||
$linesToWrite += $res.OutputLine;
|
||||
Write-Output $res.OutputLine;
|
||||
}
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||
|
||||
Set-Content -Path ($singleOutFile) -Value $linesToWrite -Encoding utf8;
|
||||
}
|
||||
# Auto file for explicit individual files (No wildcard)
|
||||
else {
|
||||
$results = $allItems | ForEach-Object -Parallel {
|
||||
$item = $_
|
||||
$algo = $using:Algorithm
|
||||
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
|
||||
$totalFiles = $allItems.Count;
|
||||
$currentIndex = 0;
|
||||
|
||||
$parallelResults = Invoke-ParallelHashing -Items $allItems -Algorithm $Algorithm;
|
||||
foreach ($res in $parallelResults) {
|
||||
$currentIndex++;
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||
|
||||
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||
if (-not $relativePath) { $relativePath = $item.Name; };
|
||||
if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
|
||||
$relativePath = $relativePath.Substring(2);
|
||||
};
|
||||
|
||||
[PSCustomObject]@{
|
||||
OutputLine = "${hash} ${relativePath}"
|
||||
ItemOutFile = "$($item.FullName).$($algo.ToLower())"
|
||||
}
|
||||
} -ThrottleLimit 16;
|
||||
|
||||
foreach ($res in $results) {
|
||||
Write-Output $res.OutputLine;
|
||||
Set-Content -Path ($res.ItemOutFile) -Value $res.OutputLine -Encoding utf8;
|
||||
}
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||
};
|
||||
};
|
||||
}
|
||||
# Standard console output (No save flags passed)
|
||||
else {
|
||||
$linesToWrite = $allItems | ForEach-Object -Parallel {
|
||||
$item = $_
|
||||
$algo = $using:Algorithm
|
||||
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
|
||||
$totalFiles = $allItems.Count;
|
||||
$currentIndex = 0;
|
||||
|
||||
$parallelResults = Invoke-ParallelHashing -Items $allItems -Algorithm $Algorithm;
|
||||
foreach ($res in $parallelResults) {
|
||||
$currentIndex++;
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Status "Processing file $currentIndex of $totalFiles" -PercentComplete (($currentIndex / $totalFiles) * 100);
|
||||
|
||||
$relativePath = Resolve-Path -Path ($item.FullName) -Relative -ErrorAction 'SilentlyContinue';
|
||||
if (-not $relativePath) { $relativePath = $item.Name; };
|
||||
if ($relativePath.StartsWith(".\") -or $relativePath.StartsWith("./")) {
|
||||
$relativePath = $relativePath.Substring(2);
|
||||
};
|
||||
|
||||
"${hash} ${relativePath}"
|
||||
} -ThrottleLimit 16;
|
||||
|
||||
foreach ($outputLine in $linesToWrite) {
|
||||
Write-Output $outputLine;
|
||||
Write-Output $res.OutputLine;
|
||||
}
|
||||
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user