dongrong6235 2017-07-21 06:41
浏览 112

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

  • 写回答

2条回答 默认 最新

  • dpfln86244 2017-07-21 07:25
    关注

    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] 
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题