Powershell Robocopy - 变量中的多个参数

I have a long robocopy string which i want to shorten a bit to make adding more robocopy processes easier for example i have this:

robocopy $source $destination /e /XC /XO /xx /tee /MIR /Z /XA:H /W:2 /R:2 /log:$ODPath/Log_$yeartime.txt /XF *%*.* *.DAT *.bak ""*#*.*"" *.ade *.adp *.asa *.ashx *.asmx *.asp *.bas *.bat *.cdx *.cer *.chm *.class *.cmd *.com *.config *.cnt *.cpl *.crt *.csh *.der *.dll *.exe *.fxp *.gadget *.grp *.hlp *.hpj *.hta *.htr *.htw *.ida *.idc *.idq *.ins *.isp *.its *.jse *.json *.ksh *.lnk *.mad *.maf *.mag *.mam *.maq *.mar *.mas *.mat *.mau *.mav *.maw *.mcf *.mda *.mdb *.mde *.mdt *.mdw *.mdz *.ms-one-stub *.msc *.msh *.msh1 *.msh1xml *.msh2 *.msh2xml *.mshxml *.msi *.msp *.mst *.ops *.pcd *.pif *.pl *.prf *.prg *.printer *.ps1 *.ps1xml *.ps2 *.ps2xml *.psc1 *.psc2 *.pst *.reg *.rem *.scf *.scr *.sct *.shb *.shs *.shtm *.shtml *.soap *.stm *.svc *.url *.vb *.vbe *.vbs *.vsix *.ws *.wsc *.wsf *.wsh *.xamlx /XD "*#*.*" "*%*.*"

However i want this:

robocopy $source $destination $parameters

Then i have multiple lines of this with different sources and destinations.

Is it possible to set the line of parameters to one variable?

I have tried:

$parameters = "/e /XC /XO /xx /tee /MIR /Z /XA:H /W:2 /R:2 /log:$ODPath/Log_$yeartime.txt /XF *%*.* *.DAT *.bak ""*#*.*"" *.ade *.adp *.asa *.ashx *.asmx *.asp *.bas *.bat *.cdx *.cer *.chm *.class *.cmd *.com *.config *.cnt *.cpl *.crt *.csh *.der *.dll *.exe *.fxp *.gadget *.grp *.hlp *.hpj *.hta *.htr *.htw *.ida *.idc *.idq *.ins *.isp *.its *.jse *.json *.ksh *.lnk *.mad *.maf *.mag *.mam *.maq *.mar *.mas *.mat *.mau *.mav *.maw *.mcf *.mda *.mdb *.mde *.mdt *.mdw *.mdz *.ms-one-stub *.msc *.msh *.msh1 *.msh1xml *.msh2 *.msh2xml *.mshxml *.msi *.msp *.mst *.ops *.pcd *.pif *.pl *.prf *.prg *.printer *.ps1 *.ps1xml *.ps2 *.ps2xml *.psc1 *.psc2 *.pst *.reg *.rem *.scf *.scr *.sct *.shb *.shs *.shtm *.shtml *.soap *.stm *.svc *.url *.vb *.vbe *.vbs *.vsix *.ws *.wsc *.wsf *.wsh *.xamlx /XD "*#*.*" "*%*.*" "

Spits out this error:

ROBOCOPY     ::     Robust File Copy for Windows                              

-------------------------------------------------------------------------------

Started : Friday, 21 July 2017 3:58:59 PM
Source - ETC
Dest - ETC

Files : 
Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30 

------------------------------------------------------------------------------

 ERROR : Invalid Parameter #3 : "/e /XC /XO /xx /tee /MIR /Z /XA:H /W:2 /R:2"

   Simple Usage :: ROBOCOPY source destination /MIR

         source :: Source Directory (drive:\path or \\server\share\path).
    destination :: Destination Dir  (drive:\path or \\server\share\path).
           /MIR :: Mirror a complete directory tree.

For more usage information run ROBOCOPY /?


****  /MIR can DELETE files as well as copy them !

And i have tried setting sections of code as different variables then putting into a array/SPLAT:

$parameters = @{$logdestination, $exclusions, $preexclusions)

However this gives an error and does not recognise the whole line as parameters. I have tried adding " " and + between each parameter but none have worked so far.

End result is supposed to be:

robocopy $destination1 $source1 $parameters
robocopy $destination2 $source2 $parameters

etc. for at least different desintations/sources

Is it just not possible?

Thanks, Steve

Hello! You can try something like this (maybe it help =) ):

$source = "D:\csvs"
$destination = "D:\csvs\folder"
$parameters = "/e /XC /XO /xx /tee /MIR /Z /XA:H /W:2 /R:2" + 
"/log:$ODPath/Log_$yeartime.txt /XF *%*.* *.DAT *.bak ""*#*.*"" *.ade *.adp *.asa *.ashx *.asmx"+
" *.asp *.bas *.bat *.cdx *.cer *.chm *.class *.cmd *.com *.config *.cnt *.cpl *.crt *.csh *.der"+
" *.dll *.exe *.fxp *.gadget *.grp *.hlp *.hpj *.hta *.htr *.htw *.ida *.idc *.idq *.ins *.isp *.its"+
" *.jse *.json *.ksh *.lnk *.mad *.maf *.mag *.mam *.maq *.mar *.mas *.mat *.mau *.mav *.maw *.mcf *.mda"+
" *.mdb *.mde *.mdt *.mdw *.mdz *.ms-one-stub *.msc *.msh *.msh1 *.msh1xml *.msh2 *.msh2xml *.mshxml *.msi"+
" *.msp *.mst *.ops *.pcd *.pif *.pl *.prf *.prg *.printer *.ps1 *.ps1xml *.ps2 *.ps2xml *.psc1 *.psc2 *.pst"+
" *.reg *.rem *.scf *.scr *.sct *.shb *.shs *.shtm *.shtml *.soap *.stm *.svc *.url *.vb *.vbe *.vbs *.vsix *.ws"+
" *.wsc *.wsf *.wsh *.xamlx /XD" + '"*#*.*" "*%*.*"'

for ($i=0; $i -le $parameters.ToString().Split(" ").Count ; $i++) 

{

robocopy $source $destination $parameters.ToString().Split(" ")[$i] 

}

What you are looking for is parameter splatting.

You'll have to create an array of parameters:

$parameters = '/e','/XC','/XO','/xx','/tee','/MIR','/Z','/XA:H', ...

And then you can pass those to robocopy.

robocopy $destination2 $source2 @parameters

Note the @ sign. That is necessary instead of a dollar sign.