解决方案并不那么容易,-Exclude 开关排除文件夹但不排除子项目,-Filter 开关使用通配符但将单个字符串作为参数..(顺便说一句,@whatever 在他说 Copy-recurse 开关时是正确的-项目取消您对 Get-ChildItems 的排除)
最后我以一种肮脏但有效的方式解决了它:
Get-ChildItem -Path $SourceFolder -Recurse | ? { $_.FullName -notlike $ReportExclude -and $_.FullName -notlike $BackupRootFolderName } | % { write-host $_.FullName; Copy-Item -Path $_.FullName -Destination $($_.FullName -replace $SourceFolder, $TargetFolder) -Force}
编辑,因为我不明白你的配置中有什么不工作,让我们用 loggin 测试一个完整的脚本:
$SourceFolder = "$env:ProgramFiles\myTest\"
$ReportExclude = "$env:ProgramFiles\myTest\Processor\Reports\*.rdl"
$TargetFolder = "$env:ProgramFiles\TargetTest"
$BackupRootFolderName = "_Backup"
$BackupRootFolderPath = "$TargetFolder\$BackupRootFolderName"
$BackupFolderPath = "$BackupRootFolderPath\$(Get-Date -format yyyyMMdd_HHmmss)"
$ErrorActionPreference = "Continue"
$WarningPreference = "Continue"
$VerbosePreference = "Continue"
Function Copy-MyFiles($FileFullNameSource)
{
try
{
Write-Verbose "file source : $FileFullNameSource"
if ($FileFullNameSource -notlike $ReportExclude -and $FileFullName -notlike $BackupRootFolderName)
{
$FileFullNameDestination = $FileFullNameSource -replace $SourceFolder, $TargetFolder
Write-Verbose "file destination : $FileFullNameDestination"
Copy-Item -Path $FileFullNameSource -Destination $FileFullNameDestination -Force
if (Test-Path $FileFullNameDestination)
{
Write-Host "$FileFullNameDestination ok" -ForegroundColor Green
}
else
{
Write-Warning "$FileFullNameDestination is missing in destination folder"
}
}
else
{
Write-Host "$FileFullNameSource is excluded"
}
}
catch
{
Write-Warning ($_.Exception | Select-Object -Property Message | Out-String)
}
}
Write-Verbose "source folder : $SourceFolder"
Write-Verbose "target folder : $TargetFolder"
Get-ChildItem -Path $SourceFolder -Recurse | % { Copy-MyFiles $_.FullName }