none
beginner issue with "unexpected token" error

    Question

  • Hi, 

    I am trying to write my first powershell script but am completely stumped and trawling google hasn't helped.

    I am writing (trying to write I should say) a script to look through a folder tree and identify photographs where the archive bit has been set and on those files run ImageMagick to convert the file to a different size.

    My script is as follows and I've included how I am running the code 

     # Invokation :
    # powershell -ExecutionPolicy RemoteSigned -File .\RI2.ps1
    
    # ----------------------------------------
    # Configuration
    $srcfolder = "D:\TempFiles\PictureProcessing\PicsforDisplay"
    $destpath = "D:\TempFiles\PictureProcessing\PicsforDisplay"
    $im_convert_exe = "c:\program files (x86)\ImageMagick-6.8.6-Q16\convert.exe"
    $src_filter = "*.jpg", "*.tif", "*.png"
    $options = "-resize 1920x1080^>"
    # ----------------------------------------
    
    foreach ($srcitem in $(Get-ChildItem ${srcfolder} -include ${src_filter} -recurse | Where {$_.attributes -match "Archive"}))
    {
        $srcname = ${srcitem}.fullname
    
        # Perform the conversion by calling an external tool
        $cmdline =  " `"" +${im_convert_exe} + "`"" + " `"" + ${srcname}  + "`"" + " "+ ${options} + " `"" + ${srcname} + "`" " 
    	echo ${cmdline} 
    	invoke-expression -command  ${cmdline}
    
    }

    When I run the script I get the error :

    Invoke-Expression : Unexpected token 'D:\TempFiles\PictureProcessing\PicsforDis
    play\Alex\2013\P1000985.JPG' in expression or statement.
    At D:\Temp\test\2\RI2.ps1:23 char:19
    +     invoke-expression <<<<  -command  ${cmdline}
        + CategoryInfo          : ParserError: (D:\TempFiles\Pi...13\P1000985.JPG:
       String) [Invoke-Expression], ParseException
        + FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.In
       vokeExpressionCommand

    I get the error once for each file (so in this instance P1000985.jpg in the given folder).

    The echo ${cmdline} entry outputs the actual text which I think is the text I am asking to be executed :

     "c:\program files (x86)\ImageMagick-6.8.6-Q16\convert.exe" "D:\TempFiles\Pictu
    reProcessing\PicsforDisplay\Alex\P1000885.JPG" -resize 1920x1080^> "D:\TempFile
    s\PictureProcessing\PicsforDisplay\Alex\P1000885.JPG"

    and if I copy that (and remove the newlines) it runs perfectly.

    I'm not sure if that is enough for anyone to help me - am I doing something incredibly dumb? Clearly I'm trying to run a dos command 

    Tuesday, July 02, 2013 11:25 PM

Answers

  • Possibly because Invoke-Expression: "The Invoke-Expression cmdlet provides one way to run a script from within Windows PowerShell"

    You want to run an exe so just supply the complete path to the exe and what ever parameters is needs, if the path has spaces, then you must enclose the path in quotes and pre-fix it with &

    C:\SomePath\Some.exe $cmd

    & "C:\Program Files\Some.exe" $cmd


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Marked as answer by TheBalance Tuesday, July 02, 2013 11:41 PM
    • Edited by clayman2 Wednesday, July 03, 2013 12:08 AM typo
    Tuesday, July 02, 2013 11:34 PM

All replies

  • I appear to have left a stone unturned -where I said that the script generated works perfectly actually I might be missing a trick as the script runs perfectly in DOS, however, Powershell is not DOS so the issue does appear to be something about how I am presenting the command to invoke-expression.

    I've just tried putting the command string above into a variable ie


    $var="`"c:\program files (x86)\ImageMagick-6.8.6-Q16\convert.exe`" `"D:\TempFiles\Pictu
    reProcessing\PicsforDisplay\Alex\P1000885.JPG`" -resize 1920x1080^> `"D:\TempFile
    s\PictureProcessing\PicsforDisplay\Alex\P1000885.JPG`""

    and then running invoke-expression -command $var and am getting the unexpected token error as before but I can't for the life of me see what I am doing wrong?

    Any pointers greatfully received.

    Thanks

    Tuesday, July 02, 2013 11:32 PM
  • Possibly because Invoke-Expression: "The Invoke-Expression cmdlet provides one way to run a script from within Windows PowerShell"

    You want to run an exe so just supply the complete path to the exe and what ever parameters is needs, if the path has spaces, then you must enclose the path in quotes and pre-fix it with &

    C:\SomePath\Some.exe $cmd

    & "C:\Program Files\Some.exe" $cmd


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    • Marked as answer by TheBalance Tuesday, July 02, 2013 11:41 PM
    • Edited by clayman2 Wednesday, July 03, 2013 12:08 AM typo
    Tuesday, July 02, 2013 11:34 PM
  • Thanks for the answer that sorted me out first try, prompt & perfect response much appreciated.
    Tuesday, July 02, 2013 11:43 PM