【发布时间】:2017-02-27 22:00:43
【问题描述】:
我正在尝试使用 PowerShell cmdlet Invoke-Expression 来启动 RoboCopy。
In the script below, RoboCopy worked fine when the option was simply '.' but as soon as the option '/MIR' was added I got this "Invalid Parameter #3" error.
RoboCopy 似乎在解析“/MIR”时遇到问题,并且在选项中的正斜杠上卡住了。我试过使用各种转义字符都无济于事!
# Source & Destination paths
#
[string]$srcPath = 'C:\folderSrc'
[string]$desPath = 'C:\folderDes'
# Example 1
# ----------
# This works - note how $option1 contains only '*.*'
#
[string]$option1 = '*.*'
[string]$line = 'RoboCopy $srcPath $desPath $option1'
Invoke-Expression "$line"
# Example 2:
# ----------
# This doesn't work - after '/MIR' is added to the option, RoboCopy seems to choke on the forward slash in '/MIR'
#
[string]$option2 = '*.* /MIR'
[string]$line = 'RoboCopy $srcPath $desPath $option2'
Invoke-Expression "$line"
【问题讨论】:
-
在我的问题中,示例 1 有效,但示例 2 无效。唯一的区别是 $option1 = '*.*' 和 $option2 = '*.* /MIR'
-
我不认为存在变量扩展问题,因为如果变量没有正确扩展,示例 1 将不起作用
-
首先请edit your question,不要添加cmets尝试修改。其次,正如您在对 Christopher G. Lewis 的回答中所分享的那样,知道这是一个解释器而不是直接的 PowerShell,我认为这是您的解释器而不是 PowerShell 本身的问题。看起来解释器没有正确处理斜线。
-
停止解析的运算符在这里可能对您有用:--% ss64.com/ps/stop-parsing.html
标签: powershell robocopy