如何让 robocopy 在 powershell 中运行?

如何让 robocopy 在 powershell 中运行?

我正在尝试在 powershell 中使用 robocopy 来镜像我家用机器上的一些目录。这是我的脚本:

param ($configFile)

$config = Import-Csv $configFile
$what = "/COPYALL /B /SEC/ /MIR"
$options = "/R:0 /W:0 /NFL /NDL"
$logDir = "C:\Backup\"

foreach ($line in $config)
{
 $source = $($line.SourceFolder)
 $dest = $($line.DestFolder)
 $logfile =  $logDIr 
 $logfile += Split-Path $dest -Leaf
 $logfile += ".log"

 robocopy "$source $dest $what $options /LOG:MyLogfile.txt"
}

该脚本接收一个包含源目录和目标目录列表的 csv 文件。当我运行该脚本时,出现以下错误:

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

  Started : Sat Apr 03 21:26:57 2010

   Source : P:\ C:\Backup\Photos \COPYALL \B \SEC\ \MIR \R:0 \W:0 \NFL \NDL \LOG:MyLogfile.txt\
     Dest - 

    Files : *.*

  Options : *.* /COPY:DAT /R:1000000 /W:30 

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

ERROR : No Destination Directory Specified.

       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 !

知道我需要做什么来修复吗?

谢谢,马克。

答案1

$what如果和的变量$options不变,为什么它们是变量?

$what = "/COPYALL /B /SEC /MIR" 
$options = "/R:0 /W:0 /NFL /NDL" 

这对我来说很好:

robocopy   $source $dest /COPYALL /B /SEC /MIR /R:0 /W:0 /NFL /NDL

答案2

如果您希望能够使用变量扩展,并且让生成的命令行正确理解您想要分隔哪些参数以及哪些参数不应该分隔,那么在 Powershell 中将字符串填充到外部命令的参数中需要一些麻烦。在您的示例中,您将整个字符串作为第一个参数发送,而 Robocopy 告诉您它找不到该长字符串作为源目录。您确实希望将整个源字符串视为一个参数(包括其中可能存在的空格),目标也是如此,但您的 $what 和 $options 将失败,因为它们都将作为无法解析的单个参数传递给 Robocopy。

有几种方法可以正确地做到这一点,但下面的代码片段基于您似乎想要分解参数的方式,并且适用于我使用的单个目录示例。

$source="C:\temp\source"
$dest="C:\temp\dest"

$what = @("/COPYALL","/B","/SEC","/MIR")
$options = @("/R:0","/W:0","/NFL","/NDL")

$cmdArgs = @("$source","$dest",$what,$options)
robocopy @cmdArgs

答案3

从 robocopy 行中删除引号?

IE

param ($configFile)

$config = Import-Csv $configFile
$what = "/COPYALL /B /SEC/ /MIR"
$options = "/R:0 /W:0 /NFL /NDL"
$logDir = "C:\Backup\"

foreach ($line in $config)
{
 $source = $($line.SourceFolder)
 $dest = $($line.DestFolder)
 $logfile =  $logDIr 
 $logfile += Split-Path $dest -Leaf
 $logfile += ".log"

 robocopy $source $dest $what $options /LOG:MyLogfile.txt
}

答案4

我发现执行本机命令的最佳方法是使用 & 命令执行字符串列表。此外,如果您希望控制台输出包含在 powershell 记录中,则需要将输出通过管道传输到 out-host。以下是使用从以下位置获取的多个参数调用 7Zip 的示例7-Zip 转 Amazon S3 Powershell 脚本我写道:

$7ZipPath = "C:\Program Files\7-Zip\7z.exe" #Path to 7Zip executable
$FolderPath = "C:\Backup" #Folder to backup (no trailing slash!)
$FolderName = $FolderPath.Substring($FolderPath.LastIndexOf("\")+1) #grab the name of the backup folder
$TimeStamp = [datetime]::Now.ToString("yyyy-MM-dd_HHmm") #Create unique timestamp string
$strFile = [String]::Format("{0}\{1}_{2}.7z", "c:\",$FolderName,$TimeStamp) #Create filename for the zip
#7z options: add, type 7z, Archive filename, Files to add (with wildcard. Change \* to \prefix* or \*.txt to limit files), compression level 9, password, encrypt filenames, Send output to host so it can be logged.
& $7ZipPath "a" "-t7z" "$strFile" "$FolderPath\*" "-mx9" "-r" "-pPASSWORD" "-mhe" | out-host #create archive file
if($LASTEXITCODE -ne 0){ #Detect errors from 7Zip. Note: 7z will crash sometimes if file already exists
    Write-Error "ERROR: 7Zip terminated with exit code $LASTEXITCODE. Script halted."
    exit $LASTEXITCODE
}

基本上,对于您的情况,我会尝试这样的事情(可能需要单独分解 $what 和 $options 参数):

$robocopypath = "c:\pathtofolder\robocopy.exe"
& $robocopypath "$source" "$dest" "$what" "$options" "/LOG:MyLogfile.txt"

相关内容