Reputation: 53
I'm trying to use the PowerShell cmdlet Invoke-Expression
to launch 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.
It seems that RoboCopy is having problems parsing '/MIR' and has choked on the forward slash in the option. I've tried using all sort of escaping characters to no avail!
# 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"
Upvotes: 1
Views: 285
Reputation: 53
I found that this (using double InvokeExpression) worked:
[string]$srcPath = 'C:\folderSrc'
[string]$desPath = 'C:\folderDes'
[string]$option = '*.* /MIR'
[string]$line = 'Invoke-Expression "RoboCopy $srcPath $desPath $option"'
Invoke-Expression "$line"
But couldn't explain why this (using single Invoke-Expression) also works:
[string]$srcPath = 'C:\folderSrc'
[string]$desPath = 'C:\folderDes'
[string]$option = '*.*'
[string]$line = 'RoboCopy $srcPath $desPath $option'
Invoke-Expression "$line"
Note that the sole difference in the 2 scenarios is the $option variable:
'*.*' vs. '*.* /MIR'
Inconsistency like this is utterly demoralizing...
Upvotes: 1
Reputation: 4836
Powershell doesn't expand $variables when using single quotes.
Use double quotes here:
[string]$line = "RoboCopy $srcPath $desPath $option1"
And it might make better sense to not use Invoke-Expression
RoboCopy.exe $srcPath $desPath *.* /MIR
Should work
Upvotes: 0