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

时间:2015-06-02 05:05:07

标签: powershell-v3.0

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

可能我错过了一些或者没有正确完成,因为我是PowerShell脚本的初学者。

任何人都可以请求帮助,下面是我尝试过的代码:

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

1 个答案:

答案 0 :(得分:0)

获得以下代码片段,用于维护子文件夹结构:

$ sourceDir = “\服务器1 \ SourceFolder \” $ TARGETDIR = “\服务器2 \ DestinationFolder \”

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

if(!($ dest.Contains('。')) - 和!(Test-Path $ dest))    {         mkdir $ dest    }

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

相关问题