Estou usando o PowerShell com -Parallel para acelerar um script que verifica formatos de arquivo usando o ImageMagick. O script funciona sem processamento paralelo, mas fica muito lento com um grande número de arquivos. Quando adiciono -Parallel, continuo recebendo um erro. Aqui está o código — alguém pode me ajudar a descobrir o que está errado?
Erro :
O conjunto de parâmetros não pode ser resolvido usando os parâmetros nomeados especificados. Um ou mais parâmetros estão ausentes, não são permitidos juntos ou um número insuficiente de parâmetros foi fornecido para o conjunto de parâmetros selecionado.
Código:
$files | ForEach-Object -Parallel {
param ($file, $magickPath, $errorLogFile)
try {
# Run ImageMagick to identify the format
$output = & $magickPath identify -ping -quiet -format "%m" $file.FullName
# Check if the image is NOT JPEG or PNG
if ($output -ne "JPEG" -and $output -ne "PNG") {
$fileName = $file.Name
"$fileName - $output"
}
}
catch {
# Log errors
$errorMsg = "Error processing file: $($file.FullName). Error: $($_.Exception.Message)"
$errorMsg | Out-File -FilePath $errorLogFile -Append
}
} -ThrottleLimit 8 -ArgumentList $magickPath, $errorLogFile | ForEach-Object {
if ($_ -ne $null) {
$nonJpegPngFiles += $_
}
}
Atualizar:
# Folder where the images are located
$folderPath = "C:\Users\johndoe\Documents\saved images"
# File where unsupported image names will be saved
$outputFile = "C:\Users\johndoe\Documents\Unsupported list\unsupported_images_list.txt"
# Path to ImageMagick executable
$magickPath = "C:\Program Files\ImageMagick-7.1.1-016\magick.exe"
# Get all files in the folder
$files = Get-ChildItem -Path $folderPath -File
# Process files in parallel
$result = $files | ForEach-Object -Parallel {
# Run ImageMagick to identify the format, redirecting stderr to stdout
$output = & $using:magickPath identify -ping -quiet -format '%m' $_.FullName 2>&1
# Return the file path and output for filtering later
[pscustomobject]@{
Path = $_.FullName
Output = $output
}
} -ThrottleLimit 8
# Filter files that are not JPEG or PNG
$unsupportedFiles = $result | Where-Object { $_.Output -notin 'JPEG', 'PNG' }
# If unsupported files exist, write them to the output file
if ($unsupportedFiles.Count -gt 0) {
$unsupportedFiles | Select-Object -ExpandProperty Path | Out-File -FilePath $outputFile
Write-Host "List of unsupported files has been saved to $outputFile."
} else {
Write-Host "No unsupported files found."
}