none
What's wrong with this invoke-expression

    Domanda

  • Hi, 

    I am getting this error from my code:

    $folder = get-date -uformat "%Y%m%d%A"
    $root = 'c:\'
    $extension = '.zip'
    $datedFolder = $root + $folder + $extension
    $exepath ='"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"'

    $switches = ' -verb:sync -source:webServer,computerName=localhost -dest:package='

    $command = $switches + $datedFolder

    invoke-expression "'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe' $command"

    I've tried every single possible way of arranging quotes around file system and also sytax for invoke-expression but getting this error

    PS C:\users\bennettj\desktop> .\IISBackup.ps1
    Invoke-Expression : You must provide a value expression on the right-hand side of the '-' operator.
    At C:\users\bennettj\desktop\IISBackup.ps1:12 char:18
    + invoke-expression <<<<  "'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe' $command"
        + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
        + FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand

    Any ideas?

    mercoledì 27 giugno 2012 09:55

Risposte

Tutte le risposte

  • Try it like this:

    invoke-expression "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe $command"


    Grant Ward, a.k.a. Bigteddy

    mercoledì 27 giugno 2012 10:19
  • Mmm, now I get this

    PS C:\users\bennettj\desktop> .\IISBackup.ps1
    The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:11
    + C:\Program <<<<  Files\IIS\Microsoft Web Deploy\msdeploy.exe  -verb:sync -source:webServer,computerName=localhost -dest:package=c:\20120627Wednesday.zip
        + CategoryInfo          : ObjectNotFound: (C:\Program:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    PS C:\users\bennettj\desktop>

    mercoledì 27 giugno 2012 10:29
  • Come to think of it, why are you using Invoke-Expression.  You shouldn't need to.  Just run the command directly from the Powershell command line, with no quotes, except around the file name and path:

     & "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" $command


    Grant Ward, a.k.a. Bigteddy


    • Modificato Bigteddy mercoledì 27 giugno 2012 12:00 Edited according to Bartek's suggestion
    mercoledì 27 giugno 2012 10:41
  • Without call operator that syntax won't work... ;)

    & "Path to exec" $parameter

    With iex: iex "& 'path to exec' $parameter" should do the job.

    • Proposto come risposta Bigteddy mercoledì 27 giugno 2012 11:59
    mercoledì 27 giugno 2012 11:57
  • Awsome, getting somewhere now but getting a strange error which looks like it is again related to the spaces this time in the parameters

    set-location 'C:\Program Files\IIS\Microsoft Web Deploy\'
    $folder = get-date -uformat "%Y%m%d%A"
    $root = 'c:\'
    $extension = '.zip'
    $datedFolder = $root + $folder + $extension
    $exepath ='"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"'

    $switches = '-verb:sync -source:webServer,computerName=localhost -dest:package='

    $command = $switches + $datedFolder

    invoke-expression "& 'C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe' $command"

    PS C:\users\bennettj\desktop> .\IISBackup.ps1
    Error: Unrecognized argument '"-source:webServer computerName=localhost"'. All arguments must begin with "-".
    Error count: 1.
    PS C:\Program Files\IIS\Microsoft Web Deploy>

    mercoledì 27 giugno 2012 12:18
  • This looks suspicious:

    Error: Unrecognized argument '"-source:webServer computerName=localhost"'. All arguments must begin with "-".

    Shouldn't it be -computerName=localhost :

    $switches = '-verb:sync -source:webServer -computerName=localhost -dest:package='


    Grant Ward, a.k.a. Bigteddy

    mercoledì 27 giugno 2012 12:21
  • To be honest in situations like this one - I would suggest using Jaykul's tip on running commands with some complicated parameters. You are getting errors is because PS will try to parse your code and "guess" what you want to do, and that may be something different than your actual command (msdeploy) requires.

    Post can be found here.

    mercoledì 27 giugno 2012 12:25
  • Hi,

    Based on my knowledge, - paramether should be like this pattern:

    -parametername parametervalue.

    So how about:

    $switches = '-verb sync -source webserver -computername localhost -dest'

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    martedì 3 luglio 2012 05:39