Powershell,robocopy数组:无效参数&时间戳保留

时间:2015-04-27 17:23:56

标签: powershell robocopy

我有这个Powershell代码:

Function CheckFileList()
{
    $limit = (Get-Date).AddDays(-270)
    $input_path = gci '//network/sourceDir' | sort -property LastWriteTime
    $output_file = 'c:\PowershellScripts\prune_results.txt'
    #Clear-Content $output_file
    $countf = 0
    $outputstr = ""

    $outputstr = $(Get-Date -format 'F') + " - Folders to be purged:`r`n"

    $input_path | Foreach-Object{
        if ( (Get-Item $_.FullName) -is [System.IO.DirectoryInfo] ) {
            if ( $_.LastWriteTime -le $limit ) {
                $source='//network/sourceDir' + $_.Name
                $dest="\\computer\c$\targetDir" + $_.Name
                $what=@("/MOVE")
                $options=@("/COPY:DAT /DCOPY:T")
                $cmdArgs = @("$source","$dest",$what,$options)
                #"robocopy " + $cmdArgs >> $output_file
                robocopy @cmdArgs
                $outputstr = $outputstr + " (" + $_.LastWriteTime + ") `t" + $_.Name + "`r`n"
                $countf++
                $outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
                $outputstr >> $output_file
                Exit
            }
        }
    }

    $outputstr = $outputstr + "Folders [to be] purged: " + $countf + "`r`n`r`n"
    $outputstr >> $output_file

}

CheckFilelist

这是为了在保留文件夹时间戳的同时移动许多文件夹(及其中的文件)。

当我运行它时,我收到此错误:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Mon Apr 27 13:20:35 2015

   Source - \\network\sourceDir\someFolder12345\
     Dest - \\computer\c$\someFolder12345\

    Files :
  Options : /COPY:DAT /MOVE /R:1000000 /W:30

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

ERROR : Invalid Parameter #4 : "/COPY:DAT /DCOPY:T"

       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 !

我的what / options数组有什么问题吗?这些参数看起来对我有用。

[编辑]我也发现这个脚本没有保留文件夹时间戳。 someFolder12345以“现在”的日期/时间结束在targetDir上。文件夹中的文件是保留时间戳,但不是文件夹?

1 个答案:

答案 0 :(得分:2)

看起来您的字符串"/COPY:DAT /DCOPY:T"作为一个参数传递给robocopy,而不是作为2个单独的参数传递。如果检查$options变量,它在数组中只有一个项目。尝试将该行更改为$options=@("/COPY:DAT","/DCOPY:T"),以便分别传递每个参数。