复制项目如何处理错误 [英] How handle errors with copy-item

查看:102
本文介绍了复制项目如何处理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个备份脚本,可以将文件的多个目录复制到备份位置。不幸的是,并非文件夹中的所有文件都可以访问。历史记录就是它们已存档,剩下的是带有灰色x的文件名。当我尝试复制它时,出现以下消息:

I have a backup script that copies several directories of files to a backup location. Unfortunately, not all of the files in the folder are accessible. What the history is, is that they were archived, and what's left is a filename with a grey x on it. When I try to copy it, I get the following message:

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

是的,它在收件人位置抱怨,不是发件人位置。我已经打开目录/文件,所以它不是只读的,但仍然可以读取。

Yes, it's complaining at the To Location, not the From Location. I have opened up the directory/files so it's not read only, but still get this.

此外,解决这些复制错误还需要很长时间。如果我可以避免首先复制这些文件,那将更快。这些文件可能已存档200个。

Also, getting through these copy errors takes a really long time. If I could avoid trying to copy these files in the first place, it would be much quicker. There's probably 200 of these files that are archived.

但是,我是在复制文件夹,而不是单独复制文件名。该文件夹中没有存档文件。没有清理它们的计划。我试图确定何时发生错误,但是只有在$ error.Exception -ne $ null语句将错误写入屏幕并永久失败之后,它才会达到断点(见注释)。

However, I'm copying the folder, not the filenames individually. There are ones that aren't archived in that folder. There isn't a plan to clean them up. I'm trying to identify when an error occurs, but it's only hitting my breakpoint if $error.Exception -ne $null statement after it writes the errors to screen and takes forever failing (see comment).

有什么主意我可以过滤出已归档的文件,或者获取它们并对照数组或列表进行检查,这样我就不会收到错误消息?自从复制整个文件夹以来,我还没有弄清楚如何找到它们。

Any idea how I can either filter out the ones that are archived, or grab them and check them against an array or list so I don't get an error message? I haven't figured out how to find them as they happen since it's copying the entire folder.

我一直在查看在复制项目期间进行错误检查,但我不知道如何将其应用于我的问题。

I was looking at error checking during copy-item but I don't see how to apply that to my issue.

这是复制方法:

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的建议进行的最新尝试,但是它正在尝试复制尚未测试的文件的属性,而该文件可以复制,只是上面的目录:

This is my latest try based on what was suggested by @theo, but it's trying to copy files I hadn't tested attributes for a file I can copy, just the dir above it:

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,但是该目录中有一个文件,该副本正试图将其复制到我的dest目录中,看到它具有以下属性:file.pdf存档,SparseFile,ReparsePoint,脱机,但我没有测试该文件,我最后测试属性的是其所在的目录。

For example, I'm testing a dir at \\drive\folder\Forms\C Forms\ and it says it's a good attribute "16", but there's a file in that dir that the copy is trying to copy over to my dest dir and I'm seeing it has this for attributes: file.pdf Archive, SparseFile, ReparsePoint, Offline. But I'm not testing that file, the last thing I tested attributes for was the dir it's in.

推荐答案

在我看来,您描述的文件是脱机文件。

在您的函数中,您可以使用某些方法测试情况是否如此像这样:

To me it looks like the files you describe are Off-line files.
In your function you can test if that is the case using something like this:

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 的目录中找到的所有文件。

From your comment I understand the function was not meant for a single file, but for all files found in a directory named $SourcePath.

在这种情况下,下面的更新函数应该可以解决问题:

In that case, here's an updated function that should do the trick:

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)"
            }
        }
    }
}

如果通过处理文件表示您仍要复制文件,请改用此文件:

If by dealing with the files you mean you still want to copy them, use this instead:

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
        }
    }
}

请参见文件属性列举所有可能的属性值。

See FileAttributes Enum for all possible attribute values.

希望有所帮助

这篇关于复制项目如何处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆