Reputation: 321
I'm going to try to make this question very simple. If I run the following, it works without issue:
$localData = "C:\"
$netData = "\\OtherPC\shared\"
Start-Process "RoboCopy.exe" -argumentlist " `"$localData`" `"$netData`" "
However, as soon as I add any switches (see below), it does not work. No errors to be seen.
$localData = "C:\"
$netData = "\\OtherPC\shared\"
Start-Process "RoboCopy.exe" -argumentlist " `"$localData`" `"$netData`" /copyall"
It's been a real pain to try and fix, any help is appreciated.
EDIT: Fixed, and here is the final working syntax:
$localFiles = "C:\Users\"
$netFiles = "\\netLocation\migration\Users\"
Start-Process "RoboCopy.exe" -NoNewWindow -argumentlist " `"$localFiles\`" `"$netFiles\`" /s /r:1 /w:5 /mt:16"
Upvotes: 1
Views: 468
Reputation: 437578
As an aside: Your use of Start-Process
may be intentional to run a lengthy operation asynchronously, in a new window, but the more typical case is to execute console applications such as Robocopy.exe
synchronously, in the same window, in which case you need to call them directly (c:\path\to\some.exe ...
or & $exePath ...
) - see this answer. An alternative to asynchronous execution in a new window is to use a background job.
You have to double a trailing \
in your path arguments if they (end up) enclosed in "..."
in the command line (ultimately) submitted:
Simply define your variable values with a trailing \\
, which should make your command work:
$localData = 'C:\\'
$netData = '\\OtherPC\shared\\'
Another solution (on Windows) for file paths specifically is to insert a space between the trailing \
and the "
. This works, because the file-system WinAPI calls ignore trailing spaces in paths.
The reason that \\
is needed at the end is that RoboCopy.exe
(and, indeed, virtually all external programs) interpret sequence \"
as an escaped "
character rather to be used verbatim rather than having syntactic function.
If you need to append the extra \
programmatically, use the following:
$localDataEscaped = $localData -replace '\\$', '\\'
$netDataEscaped = $netData -replace '\\$', '\\'
Debugging tip for Start-Process
calls:
(Temporarily) add -NoNewWindow -Wait
to the call, which makes the command output appear in the same window, giving you a chance to inspect it; by default, Start-Process
runs the command in a new window (on Windows) that automatically closes when the command finishes, potentially not giving you enough time to see the output.
Upvotes: 2