我有一个备份脚本,可将文件的多个目录复制到备份位置。不幸的是,并非文件夹中的所有文件都可以访问。历史记录是它们已存档,剩下的是带有灰色x的文件名。当我尝试将其复制时,收到以下消息:
Copy-Item : Access to the path 'E:\Backup\Loc\DO\zOLD_Under Review for NOT USED_keep for now\2006-06\N.doc' is denied.
At C:\Users\me\Documents\powershellFiles\Backup.ps1:13 char:4
+ Copy-Item -Path $SourcePath -Destination $DestinationPath -Force - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (N.doc:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
是的,它是在“到”位置而不是“从”位置抱怨。我已经打开了目录/文件,所以它不是只读的,但仍然可以读取。
此外,解决这些复制错误会花费很长时间。如果我可以避免首先尝试复制这些文件,那将更快。这些文件中可能有200个已存档。
但是,我是在复制文件夹,而不是单独复制文件名。有一些未存档在该文件夹中。没有计划清理它们。我试图确定何时发生错误,但是只有在$ error.Exception -ne $ null语句将错误写入屏幕并永久失败之后,它才会打到我的断点。
有什么主意我可以过滤出已归档的文件,或获取它们并对照数组或列表进行检查,这样我就不会收到错误消息?自从复制整个文件夹以来,我还没有弄清楚如何找到它们。
我一直在查看error checking during copy-item,但看不到如何将其应用于我的问题。
这是复制方法:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath))
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
这是我根据@theo提出的建议进行的最新尝试,但是它正在尝试复制我尚未测试的文件的属性,而该文件我可以复制的文件只是该目录上方的目录:
function CopyFileToFolderUNC($SourcePath, $DestinationPath, $exclude){
if(-Not (Test-Path $SourcePath )) #-PathType Container
{
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
write-output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
#$tempFileName = [System.IO.Path]::GetFileName($SourcePath)
#$tempDestFileNamePath = "$DestinationPath\$tempFileName"
Get-ChildItem -Path $SourcePath -Recurse -Force | ForEach {$_} {
#test if maybe we are dealing with an off-line file here
#or use the enum name 'Offline'
# or use the numeric value: 4096
#$oldAttribs = $null
$attr = $_.Attributes.value__
write-output "++ $_ $attr ++"
if (($_.Attributes -eq [System.IO.FileAttributes]::Offline) -or ($_.Attributes.value__ -eq "4096")) {
$_.Attributes=[System.IO.FileAttributes]::Normal
#$oldAttribs = $_.Attributes
#make it a 'normal' file with only the Archive bit set
#$_.Attributes = [System.IO.FileAttributes]::Archive
#log that the file was an issue and copy the other ones
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. Offline. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, Offline $_ ++"
} #if
elseif(($_.Attributes -eq [System.IO.Fileattributes]::Archive) -or ($_.Attributes.value__ -eq "32")) {
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. Archive. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, Archive $_ ++"
} #elseif
elseif(($_.Attributes -eq [System.IO.Fileattributes]::SparseFile) -or ($_.Attributes.value__ -eq "512")) {
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. SparseFile. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, SparseFile $_ ++"
} #elseif
elseif(($_.Attributes -eq [System.IO.Fileattributes]::ReparsePoint) -or ($_.Attributes.value__ -eq "1024")) {
$global:ErrorStrings.Add("Found offline file in backup dir, $_. Logging info and not copying this file. ReparsePoint. Please investigate.;; ")
write-output "++ Error: An error occured during copy operation. No such path or file, ReparsePoint $_ ++"
} #elseif
else {
#the file is not or no longer off-line, so proceed with the copy
$_ | Copy-Item -Destination $DestinationPath -Force -Recurse -ErrorVariable errors
foreach($error in $errors)
{
if ($error.Exception -ne $null)
{
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
write-output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++"
}
write-output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
} #else
#if ($oldAttribs) {
# $_.Attributes = $oldAttribs
#}
} #Get-ChildItem
例如,我正在\\ drive \ folder \ Forms \ C Forms \中测试一个目录,它说它是一个很好的属性“ 16”,但是该目录中有一个文件,该副本正在尝试复制到我的目标目录,我看到它具有以下属性:file.pdf存档,SparseFile,ReparsePoint,脱机。但是我没有测试该文件,我测试属性的最后一件事是它所在的目录。
答案 0 :(得分:0)
在我看来,您描述的文件是脱机文件。
在您的函数中,您可以使用以下方法测试情况是否正确:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath)) {
$global:ErrorStrings.Add("Exception: No such path, $SourcePath;; ")
Write-Output "++ Error: An error occured during copy operation. No such path, $SourcePath ++"
}
# test if maybe we are dealing with an off-line file here
if ((Get-Item -Path $SourcePath).Attributes -band 4096) { # or use the enum name 'Offline'
# or use .Net:
# [System.IO.File]::GetAttributes($SourcePath) -band 4096
Write-Output "Did not copy: File '$SourcePath' is currently off-line. "
}
else {
# the file is not off-line, so proceed with the copy
Copy-Item -Path $SourcePath -Destination $DestinationPath -Force -Recurse -errorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++" #this breakpoint is hit after giving errors on screen and taking a long time/failing to copy files it can't reach
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
根据您的评论,我了解该功能不是针对单个文件,而是针对在名为$SourcePath
的目录中找到的所有文件。
在这种情况下,下面的更新功能应该可以解决问题:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath -PathType Container)) {
$global:ErrorStrings.Add("Exception: No such path '$SourcePath'")
Write-Output "++ Error: An error occured during copy operation. No such path '$SourcePath' ++"
}
Get-ChildItem -Path $SourcePath -File | ForEach-Object {
# test if maybe we are dealing with an off-line file here
# or use the enum name 'Offline'
# or use the numeric value: 4096
if ($_.Attributes -band [System.IO.FileAttributes]::Offline) {
Write-Output "Did not copy: File '$($_.FullName)' is currently off-line."
}
else {
# the file is not off-line, so proceed with the copy
$_ | Copy-Item -Destination $DestinationPath -Force -Recurse -ErrorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++"
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
}
}
}
如果通过处理文件表示您仍要复制文件,请改用此文件:
function CopyFileToFolderUNC($SourcePath, $DestinationPath){
if(-Not (Test-Path $SourcePath -PathType Container)) {
$global:ErrorStrings.Add("Exception: No such path '$SourcePath'")
Write-Output "++ Error: An error occured during copy operation. No such path '$SourcePath' ++"
}
Get-ChildItem -Path $SourcePath -File | ForEach-Object {
# test if maybe we are dealing with an off-line file here
# or use the enum name 'Offline'
# or use the numeric value: 4096
$oldAttribs = $null
if ($_.Attributes -band [System.IO.FileAttributes]::Offline) {
$oldAttribs = $_.Attributes
# make it a 'normal' file with only the Archive bit set
$_.Attributes = [System.IO.FileAttributes]::Archive
}
# the file is not or no longer off-line, so proceed with the copy
$_ | Copy-Item -Destination $DestinationPath -Force -Recurse -ErrorVariable errors
foreach($error in $errors) {
if ($error.Exception) {
$global:ErrorStrings.Add("Exception: $($error.Exception);; ")
Write-Output "++ Error: An error occured during copy operation. Exception: $($error.Exception) ++"
}
Write-Output "Error: An error occured during copy operation. Exception: $($error.Exception)"
}
# if you want the attributes in the original file to be restored, use:
if ($oldAttribs) {
$_.Attributes = $oldAttribs
}
}
}
有关所有可能的属性值,请参见FileAttributes Enum。
希望有帮助