Powershell Command Processing (Passing in Variables)

后端 未结 5 1949
孤独总比滥情好
孤独总比滥情好 2021-02-15 23:21

I\'m creating a Powershell script to deploy some code and part of the process is to call a command-line compression tool called RAR.EXE to back-up some folders.

I\'m at

5条回答
  •  时光取名叫无心
    2021-02-15 23:41

    Your last example if failing because "&" treats the string as one argument, so it is looking for a program named "cmd /c echo foo.exe". :)

    This works:

    & $cmd $params
    

    As for the double quote issue, it does seem that cmd does not like the quotes around arguments that PowerShell puts. It gets this:

    cmd "/c echo foo"
    

    So I think it treats everything after /c as the exact command, so like so:

    echo foo"
    

    Some command line programs and funky parsing of the command line (that is why PowerShell takes over this job for functions and cmdlets). In the case of cmd, I would suggest this:

    $param = "echo foo"
    & cmd /c $param
    

提交回复
热议问题