如何使用数组在 Powershell for 循环中使用 Robocopy?

How to use Robocopy in a Powershell for loop using arrays?

我们正在迁移两个共享,假设 oldShare 到 newShare。我以前从未使用过 Robocopy,但根据我的谷歌搜索,它看起来像是我应该用于此任务的。文件目录在共享上设置不同,所以我必须使用多个 Robocopy 命令,而不仅仅是一个。使用我的基本编程知识和更多关于 Powershell 的谷歌搜索,这是我想出的脚本 运行 在新的分享:

$source_array=@("\DC02\ArchivedData\IT-Backups", "\DC02\Contractors", "\DC02\DataLoad", "\DC02\Infrastructure", "\DC02\Support")
$destination_array=@("S:\Shares\COMPANYNAME\IT\Archives", "S:\Shares\COMPANYNAME\Public\Contractors", "S:\Shares\COMPANYNAME\Dataload", "S:\Shares\COMPANYNAME\IT\Infrastructure", "S:\Shares\COMPANYNAME\Public\Support")
for ($i=0; $i -lt $source_array.length; $i++) {

    $date=(Get-Date -format dd-MM-yyyy_hh:mm:ss_tt)
    robocopy $source_array[$i] $destination_array[$i] /e /zb /copyall /r:3 /w:3 /xo /log:c:\ROBOCOPY_Logs$date.log /V /NP

}

这样看起来对吗?我只是想确保我不会把任何事情搞砸,因为我以前从未真正使用过 Robocopy 或 Powershell。

存在一些语法错误(windows 文件名中的冒号等),但 Powershell 部分一切正常。感谢/L 建议。

我的最终剧本:

$source_array=@('\DC02\ArchivedData\IT-Backups', '\DC02\Contractors', '\DC02\DataLoad', '\DC02\Infrastructure', '\DC02\Support')
$destination_array=@('S:\Shares\COMPANYNAME\IT\Archives', 'S:\Shares\COMPANYNAME\Public\Contractors', 'S:\Shares\COMPANYNAME\Dataload', 'S:\Shares\COMPANYNAME\IT\Infrastructure', 'S:\Shares\COMPANYNAME\Public\Support')

for ($i=0; $i -lt $source_array.length; $i++) {

    $date=(Get-Date -format mm-dd-yyyy_hh-mm-ss_tt)
    $filename=('robocopy_iteration' + '_' + $i + '_' + $date)

    # robocopy $source_array[$i] $destination_array[$i] /V /L /TEE /E /COPYALL /ZB /ETA /XO /R:3 /W:3 /LOG:C:\Users\USER\Desktop$filename.log
    # /L means this is a test, it won't actually copy. Rather it will tell you what will be copied, skipped, etc. 

    robocopy $source_array[$i] $destination_array[$i] /V /TEE /E /COPYALL /ZB /ETA /XO /R:3 /W:3 /LOG:C:\Users\USER\Desktop$filename.log

}