当我在下面运行此脚本时,我很高兴:
$backuplist = Get-ChildItem "E:\res\store\Backup"
ForEach ($dir in $backuplist) {
$letters= dir $dir.FullName -Recurse
$snd = $letters | Where FullName -like "*SND_BE*" -and $_.name -Like
"*Letters*" -and -$_.name l-like "*OvernightLetters*"| where-object
{$_.Name -notlike "*MailService*" -and $_.Name -notlike
"*CustomerCommunications*"
-and $_.Name -notlike "*JobQueueResults*" -and $_.name -notlike "*Logs*"
-and $_.Name -notlike "*Notifications*" -and $_.name -notlike "*Uploads*"
-and
$_.Name -notlike "*Uploads*"}
$swy = $letters | Where FullName -like "*SWY_BE*" -and $_.name -Like
"*Letters*" -and -$_.name l-like "*OvernightLetters*"| where-object
{$_.Name -notlike "*MailService*" -and $_.Name -notlike
"*CustomerCommunications*"
-and $_.Name -notlike "*JobQueueResults*" -and $_.name -notlike "*Logs*"
-and $_.Name -notlike "*Notifications*" -and $_.name -notlike "*tUploads*"
-and $_.Name -notlike "*Uploads*"}
$snd | Copy-Item -Destination E:\TempLetters\SND
$swy | Copy-Item -Destination E:\TempLetters\SWY
}
我收到此错误Copy-Item:具有指定名称E:\ Templetters \ snd的项目已存在。我能做些什么不同的事情?我怎样才能使这个脚本更有效率?谢谢你的帮助将不胜感激。感谢
答案 0 :(得分:1)
我重写了它的日志,因为你过滤了两次并获得了两次子项。但是,我修改过的脚本会写出脚本正在做什么,但实际上并没有执行操作。这将确保它正在按照您的预期进行操作。如果它执行了您期望的操作,则可以取消注释copy-item命令并再次运行它。
Get-ChildItem -Path "E:\res\store\Backup" -Recurse |
# Filter children using Where-Object
Where-Object -FilterScript {
( $_.Name -match "Letters|OvernightLetters" ) -and
( $_.Name -notmatch "MailService|CustomerCommunications|JobQueueResults|Notifications|Logs|Uploads" )
} |
# Foreach of those objects, do something
ForEach-Object -Process {
# Use a regex switch to decide what to do with the remainder
switch -Regex ( $_.FullName ) {
"*SND_BE*" {
Write-Host "Copying `"$($_.FullName)`" to `"E:\TempLetters\SND`""
$NewPath = Join-Path -Path "E:\TempLetters\SND" -ChildPath $_.Name
if ( Test-Path -Path $NewPath ) {
Write-Warning "Path `"$NewPath`" already exists."
}
# Commenting out line below that actually performs the copy
# $_ | Copy-Item -Destination E:\TempLetters\SND -Force
}
"*SWY_BE*" {
Write-Host "Copying `"$($_.FullName)`" to `"E:\TempLetters\SWY`""
$NewPath = Join-Path -Path "E:\TempLetters\SWY" -ChildPath $_.Name
if ( Test-Path -Path $NewPath ) {
Write-Warning "Path `"$NewPath`" already exists."
}
# Commenting out line below that actually performs the copy
# $_ | Copy-Item -Destination E:\TempLetters\SWY -Force
}
} # Close switch -Regex ( $_.FullName )
} # Close Foreach-Object