使用PowerShell复制源服务器相同目录结构中的文件夹和子文件夹中的项目文件 [英] Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell
问题描述
我真的很难让下面的脚本工作以正确结构复制文件夹和子文件夹中的文件(作为源服务器).
I am struggling really hard to get this below script worked to copy the files in folders and sub folders in the proper structure (As the source server).
假设,有下面提到的文件夹:
Lets say, there are folders mentioned below:
主文件夹:文件 aaa、文件 bbb
Main Folder: File aaa, File bbb
子文件夹 a:文件 1、文件 2、文件 3
Sub Folder a: File 1, File 2, File 3
子文件夹 b:文件 4、文件 5、文件 6
Sub Folder b: File 4, File 5, File 6
使用的脚本:
Get-ChildItem -Path \\Server1\Test -recurse | ForEach-Object {
Copy-Item -LiteralPath $_.FullName -Destination \\server2\test |
Get-Acl -Path $_.FullName | Set-Acl -Path "\\server2\test\$(Split-Path -Path $_.FullName -Leaf)"
}
输出:文件 aaa,文件 bbb
Output: File aaa, File bbb
子文件夹a(空文件夹)
Sub Folder a (Empty Folder)
子文件夹 b(空文件夹)
Sub Folder b (Empty Folder)
文件 1、文件 2、文件 3、文件 4、文件 5、文件 6.
File 1, File 2, File 3, File 4, File 5, File 6.
我希望将文件复制到各自的文件夹(如源文件夹).非常感谢任何进一步的帮助.
I want the files to get copied to their respective folders (Like the source folders). Any further help is highly appreciated.
推荐答案
这可以通过使用 Copy-Item 来完成.无需使用 Get-Childitem.我认为你只是想多了.
This can be done just using Copy-Item. No need to use Get-Childitem. I think you are just overthinking it.
Copy-Item -Path C:\MyFolder -Destination \\Server\MyFolder -recurse -Force
我刚刚测试了它,它对我有用.
I just tested it and it worked for me.
包含评论中的建议
# Add wildcard to source folder to ensure consistent behavior
Copy-Item -Path $sourceFolder\* -Destination $targetFolder -Recurse
这篇关于使用PowerShell复制源服务器相同目录结构中的文件夹和子文件夹中的项目文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!