waypoit debuguing

This commit is contained in:
2026-09-25 16:20:22 -06:00
parent 07a29167dd
commit 503e947eaf
+84 -61
View File
@@ -3,11 +3,12 @@ param(
# File(s) or wildcard pattern to compute checksum for # File(s) or wildcard pattern to compute checksum for
[Parameter(Position = 0, ParameterSetName = 'Generate', ValueFromPipeline = $true)] [Parameter(Position = 0, ParameterSetName = 'Generate', ValueFromPipeline = $true)]
[Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)] [Parameter(Position = 0, ParameterSetName = 'Match', ValueFromPipeline = $true)]
[Parameter(Position = 0, ParameterSetName = 'PrincessPi', ValueFromPipeline = $false)]
[string[]]$FilePath, [string[]]$FilePath,
# Custom output file (-f / -OutFile / -of) # Custom output file (-f / -OutFile / -of)
[Parameter(Mandatory = $false, ParameterSetName = 'Generate')] [Parameter(Mandatory = $false, ParameterSetName = 'Generate')]
[Alias('f', 'of', 'OF')] [Alias('f', 'of')]
[string]$OutFile, [string]$OutFile,
# Auto output file switch (-s / -Save) # Auto output file switch (-s / -Save)
@@ -37,7 +38,7 @@ param(
[switch]$Recursive, [switch]$Recursive,
# Princess Pi art flag (-pp / -PrincessPi) # Princess Pi art flag (-pp / -PrincessPi)
[Parameter(Mandatory = $false)] [Parameter(Mandatory = $true, ParameterSetName = 'PrincessPi')]
[Alias('pp')] [Alias('pp')]
[switch]$PrincessPi, [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 { process {
@@ -281,8 +311,10 @@ process {
}; };
$expectedHash = $Match.Trim().ToLower(); $expectedHash = $Match.Trim().ToLower();
$totalFiles = $allItems.Count;
$currentIndex = 0;
$allItems | ForEach-Object -Parallel { $results = $allItems | ForEach-Object -Parallel {
$item = $_ $item = $_
$algo = $using:Algorithm $algo = $using:Algorithm
$exp = $using:expectedHash $exp = $using:expectedHash
@@ -296,15 +328,21 @@ process {
TargetFile = $targetFile TargetFile = $targetFile
IsMatch = ($actualHash -eq $exp) IsMatch = ($actualHash -eq $exp)
} }
} -ThrottleLimit 16 | ForEach-Object { } -ThrottleLimit 16;
if ($_.IsMatch) {
Write-Host "$($_.TargetFile): " -NoNewline; 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; Write-Host "OK" -ForegroundColor Green;
} else { } else {
Write-Host "$($_.TargetFile): " -NoNewline; Write-Host "$($res.TargetFile): " -NoNewline;
Write-Host "FAILED" -ForegroundColor Red; Write-Host "FAILED" -ForegroundColor Red;
}; };
}; };
Write-Progress -Activity "Matching Checksums ($Algorithm)" -Completed;
} }
# --- VERIFICATION MODE (-c / -Check) --- # --- VERIFICATION MODE (-c / -Check) ---
elseif ($PSCmdlet.ParameterSetName -eq 'Verify') { elseif ($PSCmdlet.ParameterSetName -eq 'Verify') {
@@ -413,16 +451,19 @@ process {
$currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())"; $currentOutFile = Join-Path -Path ($dirPath) -ChildPath "checksums.$($Algorithm.ToLower())";
}; };
$linesToWrite = $filesInDir | ForEach-Object -Parallel { $totalFiles = $filesInDir.Count;
$item = $_ $currentIndex = 0;
$algo = $using:Algorithm $linesToWrite = @();
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower();
"${hash} $($item.Name)"
} -ThrottleLimit 16;
foreach ($outputLine in $linesToWrite) { $parallelResults = Invoke-ParallelHashing -Items $filesInDir -Algorithm $Algorithm;
Write-Output $outputLine; 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; Set-Content -Path ($currentOutFile) -Value $linesToWrite -Encoding utf8;
}; };
@@ -436,70 +477,52 @@ process {
$singleOutFile = "checksums.$($Algorithm.ToLower())"; $singleOutFile = "checksums.$($Algorithm.ToLower())";
}; };
$linesToWrite = $allItems | ForEach-Object -Parallel { $totalFiles = $allItems.Count;
$item = $_ $currentIndex = 0;
$algo = $using:Algorithm $linesToWrite = @();
$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;
foreach ($outputLine in $linesToWrite) { $parallelResults = Invoke-ParallelHashing -Items $allItems -Algorithm $Algorithm;
Write-Output $outputLine; 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; Set-Content -Path ($singleOutFile) -Value $linesToWrite -Encoding utf8;
} }
# Auto file for explicit individual files (No wildcard) # Auto file for explicit individual files (No wildcard)
else { else {
$results = $allItems | ForEach-Object -Parallel { $totalFiles = $allItems.Count;
$item = $_ $currentIndex = 0;
$algo = $using:Algorithm
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $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; Write-Output $res.OutputLine;
Set-Content -Path ($res.ItemOutFile) -Value $res.OutputLine -Encoding utf8; Set-Content -Path ($res.ItemOutFile) -Value $res.OutputLine -Encoding utf8;
} }
Write-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
}; };
}; };
} }
# Standard console output (No save flags passed) # Standard console output (No save flags passed)
else { else {
$linesToWrite = $allItems | ForEach-Object -Parallel { $totalFiles = $allItems.Count;
$item = $_ $currentIndex = 0;
$algo = $using:Algorithm
$hash = (Get-FileHash -Path ($item.FullName) -Algorithm ($algo)).Hash.ToLower(); $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'; Write-Output $res.OutputLine;
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-Progress -Activity "Computing Checksums ($Algorithm)" -Completed;
}; };
} }
} }