简体   繁体   中英

Invalid Parameters in Powershell Script

Sorry-- newbie question from a botanist ... :-P

We have a database that outputs original sized JPEG files of plants to a folder. We also deliver output of the database to the web, and would like to reference these images so that people know what the plants look like. (see this webpage , search for Magnolia) We reduce the size of the original images with ImageMagick to 'web' and 'thumbnail' sized images, stored in respective folders.

Using PowerShell, I am getting an error that I have been unable to track down, but I am sure has a simple solution. I've looked for the error on different websites for a couple of hours, but feel that someone with more knowledge can answer.

Error message:

*convert : Invalid Parameter - 210x210
At line:1 char:1
+ convert  -resize '210x210' -compress JPEG -quality '85' DSCN4568.JPG C:\images\t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Invalid Parameter - 210x210:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError*

I have also rearranged the statement passed in, and basically PS reports every third item in the command line as an error.

Here is my code (but mangled by my attempted corrections...):

    $error_log = "C:\images\error_log.txt"
    $originals = "C:\images\originals"
    $count = (Get-ChildItem ).Count
    $date = Get-Date

    Add-Content $error_log "\n----------------------------------\n"
    Add-Content $error_log "Processing $count files on $date\n\n"

    $images = Get-ChildItem "C:\images\originals\*.JPG"


    ForEach ($image in Get-ChildItem -Path "C:\images\originals" -File) {
      if(Test-Path $image.FullName) {

            # Check if there is a thumbnail
        if(Test-Path "C:\images\thumbnail\$image") {
            # Do nothing
        } else {
            # location: C:\Program Files\ImageMagick-6.9.1-Q16\convert.exe
        $command = "convert  -resize '210x210' -compress JPEG -quality '85' $image C:\images\thumbnail\$image"
            # Write-Host $command"
        Invoke-Expression $command
        }


       if(Test-Path "C:\images\web\$image") {
            # Do nothing
        } else {
            # location: C:\Program Files\ImageMagick-6.9.1-Q16\convert.exe
        Invoke-Expression "convert -resize '800x800' -compress JPEG -quality 85 $image C:\images\web\$image"
        }

      } else {
            # write to error log
        Add-Content $error_log "Unable to find $image \n"
      }
    }

Please let me know if I can clarify this any further. I am after all, a botanist, not a social maven. :-) Sincere gratitude for any potential answers.

Tony

Etan Reisner was on the right track for sure. I'm confident the issue is you are not running the convert.exe you think you are either. If you use the following command you can see why

PS C:\Users\Matt> get-command convert | Select-Object Path

Path                                                                                                                               
----                                                                                                                               
C:\Windows\system32\convert.exe  

I am sure you will have a similar result. The convert.exe you want is not in the Path environment variable but the convert which is a "File System Conversion Utility" is. Since you already know the full path to your exe I think you just need to use that.

# This only needs to be declared once at the beginning
$exePath = "C:\Program Files\ImageMagick-6.9.1-Q16\convert.exe"
#.... Other code stuff
$command = "$exePath -resize '210x210' -compress JPEG -quality '85' $image C:\images\thumbnail\$image"
Invoke-Expression $command

While having a look at the code I just wanted to mention that while error prevention is a great idea the if statement if(Test-Path $image.FullName) { is redundant. You already know the file exists because get-ChildItem found it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM