Powershell with Robocopy Passing and Arguments

I am trying to write a script that uses robocopy . If I just did it manually, my command would be:

 robocopy c:\hold\test1 c:\hold\test2 test.txt /NJH /NJS 

BUT, when I do this from powershell, like:

 $source = "C:\hold\first test" $destination = "C:\hold\second test" $robocopyOptions = " /NJH /NJS " $fileList = "test.txt" robocopy $source $destination $fileLIst $robocopyOptions 

I got:

 ------------------------------------------------------------------------------- ROBOCOPY :: Robust File Copy for Windows ------------------------------------------------------------------------------- Started : Fri Apr 10 09:20:03 2015 Source - C:\hold\first test\ Dest - C:\hold\second test\ Files : test.txt Options : /COPY:DAT /R:1000000 /W:30 ------------------------------------------------------------------------------ ERROR : Invalid Parameter #4 : " /NJH /NJS " 

However, if I change the robocopy command to

 robocopy $source $destination $fileLIst /NJH /NJS 

everything works successfully.

So my question is, how can I pass the string as parameters of my robocopy command (and, in a broader sense, do the same for any given external command)

+9
source share
3 answers

Start robocopy -args "$source $destination $fileLIst $robocopyOptions"
or
robocopy $source $destination $fileLIst $robocopyOptions.split(' ')

+15
source

Use arrays, Luke. If you specify an array of values, PowerShell will automatically expand them into separate parameters. In my experience, this is the most reliable method. And this does not require you to mess with the Start-Process cmdlet, which, in my opinion, is unnecessary for such tasks.

This trick is taken from the best article about PowerShell's behavior in relation to external executables: PowerShell and external commands performed correctly .

Example:

 $source = 'C:\hold\first test' $destination = 'C:\hold\second test' $robocopyOptions = @('/NJH', '/NJS') $fileList = 'test.txt' $CmdLine = @($source, $destination, $fileList) + $robocopyOptions & 'robocopy.exe' $CmdLine 
+6
source

You cannot use a string to pass parameters this way, because when you write

 robocopy $source $destination $fileList $robocopyOptions 

PowerShell will evaluate the last variable ( $robocopyOptions ) as one line, and it will quote it. This means that robocopy will receive "/NJH /NHS" (single line, quotation mark) at the command line. (Obviously, this is not an intention.)

For more information on how to get around these issues, see here:

http://windowsitpro.com/powershell/running-executables-powershell

The article contains the following function:

 function Start-Executable { param( [String] $FilePath, [String[]] $ArgumentList ) $OFS = " " $process = New-Object System.Diagnostics.Process $process.StartInfo.FileName = $FilePath $process.StartInfo.Arguments = $ArgumentList $process.StartInfo.UseShellExecute = $false $process.StartInfo.RedirectStandardOutput = $true if ( $process.Start() ) { $output = $process.StandardOutput.ReadToEnd() ` -replace "\r\n$","" if ( $output ) { if ( $output.Contains("`r`n") ) { $output -split "`r`n" } elseif ( $output.Contains("`n") ) { $output -split "`n" } else { $output } } $process.WaitForExit() & "$Env:SystemRoot\system32\cmd.exe" ` /c exit $process.ExitCode } } 

This feature will allow you to run the executable in the current console window, and also allow you to build an array of string parameters to pass it.

So, in your case, you can use this function something like this:

 Start-Executable robocopy.exe $source,$destination,$fileList,$robocopyOptions 
+4
source

Source: https://habr.com/ru/post/984952/


All Articles