Powershell Command Processing (Passing in Variables)

后端 未结 6 2205
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 23:01

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

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-15 23:55

    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
    

提交回复
热议问题