Powershell复制项目未保留文件夹结构

Powershell复制项目未保留文件夹结构,powershell,Powershell,我使用下面的脚本将数据从本地文件夹复制到使用当前日期创建的远程文件夹。但是,文件正在复制,但文件夹结构未复制 $Date = (Get-Date).ToString("MMddyyyy"),$_.Extension $Source = "E:\Folder1\\*" $Dest = "\\Server\Share\Folder2" $Username = "Username" $Password = ConvertTo-SecureString "Password" -AsPlainText

我使用下面的脚本将数据从本地文件夹复制到使用当前日期创建的远程文件夹。但是,文件正在复制,但文件夹结构未复制

$Date = (Get-Date).ToString("MMddyyyy"),$_.Extension
$Source = "E:\Folder1\\*"
$Dest   = "\\Server\Share\Folder2"
$Username = "Username"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
Remove-PSDrive -Name T
Start-Sleep -s 1
New-PSDrive -Name T -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
if (!(Test-Path "T:\$Date"))
{
    md -Path "T:\$Date"
}
Get-ChildItem -Path $Source -Recurse | % { Copy-Item -Path $_ -Destination "T:\$Date" -Container -Force -Verbose }
有人能告诉我哪里出了问题吗


谢谢。

剧本不错,我想我们可以马上把它整理好

为什么会这样 失败的原因就在这一步:

  Get-ChildItem -Path $Source -Recurse  
-Recurse开关正在给您带来痛苦。为了说明原因,我创建了一个简单的文件夹结构

单独运行Get-ChildItem-Path$Source-Recurse时,将得到$Source路径中所有文件的递归列表,如下所示:

PS C:\temp\stack> Get-ChildItem -Recurse

Directory: C:\temp\stack

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         8/4/2017  10:50 AM                Source


Directory: C:\temp\stack\Source    

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         8/4/2017  10:57 AM                1
d-----         8/4/2017  10:57 AM                2
d-----         8/4/2017  10:57 AM                3
d-----         8/4/2017  10:57 AM                4


Directory: C:\temp\stack\Source\1    

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         8/4/2017  10:57 AM             20 Archive.rar
-a----         8/4/2017  10:56 AM              0 File01.bmp
好的,在脚本的下一步中,您将通过管道将输出传递到每个文件的复制项

基本上,您明确告诉PowerShell‘获取此文件夹及其所有子文件夹和所有内容,并将其全部转储到这一文件夹中,忽略文件夹结构’

如何修复 您真正想要做的只是将-Recurse参数移到Copy Item上,就完成了:

Get-ChildItem -Path $Source |  
    Copy-Item -Destination "T:\$Date" -Container -Recurse -Force -Verbose 

希望对您有所帮助,非常感谢您提供的建议FoxDeploy,当我将-Recurse移到上面时,不幸地看到以下错误:<目标文件T:\10312017\文件夹是一个目录,而不是一个文件>,复制失败。