使用 Windows PowerShell 脚本将文件夹内容从一台服务器复制到另一台服务器

Copying folder contents from one server to another using Windows PowerShell scripting

我正在尝试使用 PowerShell 脚本将文件夹内容从服务器 1 复制到服务器 2。代码有效,但子文件夹中的文件被复制到它们之外。

由于我是 PowerShell 脚本的初学者,可能我遗漏了某些东西或没有正确完成。

有人可以帮忙吗,下面是我试过的代码:

$sourceDir="\Server1\SourceFolder\"
$targetDir="\Server2\DestinationFolder\"
Get-ChildItem -Recurse $sourceDir |Copy-Item -Destination $targetDir
Write-Host Copied

获得了以下维护子文件夹结构的片段:

$sourceDir="\Server1\SourceFolder\" $targetDir="\Server2\DestinationFolder\"

Get-ChildItem $sourceDir -递归 | %{ $dest = $targetDir + $_.FullName.SubString($sourceDir.Length)

If (!($dest.Contains('.')) -and !(Test-Path $dest)) { mkdir $目标 }

Copy-Item $_.FullName -Destination $dest -Force }