如何从 Powershell 脚本启动 Robocopy

How to start Robocopy from a Powershell Script

我正在尝试将 C:\ 驱动器中的用户数据从联网计算机传输到本地计算机上的 C:\ 驱动器。所以我需要排除所有系统文件夹和隐藏文件夹。

这就是我的 powershell 脚本。

$asset = Read-Host "What is the Asset Number?"
$useraiu = Read-Host "What is the user's AIU?"

$source = "\$asset\C$\"
$dest = "C:\Temp\Dest"

$excludedFolders = '_SMSTaskSequence','inetpub','Program Files','Program Files (x86)','Swsetup','Users','Windows','CCM','Notes'

$myFolders = Get-ChildItem  -Path "\$asset\C$\" -Directory | where { $_ -notin $excludedFolders }

$myFolders | foreach { robocopy $_  $dest /S /Z /MT /XJD /XA:SH /log+:"\server\path\path\%asset%-to-%computername%-Transfer-Log.log" /NP /FP /V /TEE}

Read-Host "Press ENTER to quit"

当我执行 Write-Output $myFolders 时,它会显示我要复制的正确目录。

看起来 robocopy 函数正在使用本地 C:\ 路径作为源。来源应该是 \$asset\C$\ $myFolders

中列出的文件夹

例子。如果此文件夹在旧计算机上 \computername\C$\Userfolder-Pictures 使用 Robocopy 将该文件夹复制到 C:\Userfolder-Pictures 在本地计算机上

我在 运行 脚本后从 Robocopy 得到了这个输出。

 Log File : \server\path\path\%asset%-to-%computername%-Transfer-Log.log

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

  Started : Mon Jun 08 11:11:28 2015

   Source : C:\WINDOWS\system32\Temp\
     Dest : C:\Temp\Dest\

    Files : *.*

  Options : *.* /V /FP /TEE /S /COPY:DAT /Z /NP /XJD /XA:SH /MT:8 /R:1000000 /W:30 

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

2015/06/08 11:11:28 ERROR 2 (0x00000002) Accessing Source Directory C:\WINDOWS\system32\Temp\
The system cannot find the file specified. 

添加 Select-Object -ExpandProperty FullName 将为您提供字符串数组中文件夹的完整网络路径。您的 foreach 循环 $myFolders 当前正在循环 DirectoryInfo 对象数组。

$myFolders = Get-ChildItem  -Path "\$asset\C$\" -Directory | where { $_ -notin $excludedFolders } | Select-Object -ExpandProperty FullName

或者,如果您计划使用 $myFolders 中的信息而不是 FullName,您可以将此行更改为使用 $_.FullName.

$myFolders | foreach { robocopy $_.FullName  $dest /S /Z /MT /XJD /XA:SH /log+:"\server\path\path\%asset%-to-%computername%-Transfer-Log.log" /NP /FP /V /TEE}