带超时的复制项目 [英] Copy-Item with timeout

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

问题描述

我正在从硬盘恢复文件,其中一些文件无法读取.我无法更改硬件级别的超时/ERC,并且当我有几十万个文件时很难解决,其中任何几万个文件都可能无法读取.

I am recovering files from a hard drive wherein some number of the files are unreadable. I'm unable to change the hardware level timeout / ERC, and it's extremely difficult to work around when I have several hundred thousand files, any tens of thousands of which might be unreadable.

数据问题是控制器故障的结果.购买匹配的驱动器(一路向下),我已经能够访问该驱动器,并且可以毫无问题地复制大量的驱动器.但是,整个驱动器中散布着无法读取的文件,当访问这些文件时,会导致 SATA 总线挂起.我使用过各种可恢复的文件复制应用程序,例如 robocopy、RichCopy 和其他十几个应用程序,但它们都有相同的问题.它们有一个基于实际从驱动器报告错误的重试计数.问题是驱动器报告错误需要很长时间,这意味着单个文件可能需要长达一个小时才能正式失败.我知道每个文件应该有多快,所以我想构建一个 powershell CMDLET 或类似的东西,它允许我传入源文件名和目标文件名,并让它尝试复制文件.如果 5 秒后文件没有被复制(或者如果有 - 这可能是一个愚蠢的过程),我希望它退出.我将编写一个单独触发每个复制进程的脚本,等待该进程完成,但到目前为止我无法找到一种对进程设置时间限制的好方法.

The data issues were the result of a controller failure. Buying a matching drive (all the way down), I've been able to access the drive, and can copy huge swaths of it without issues. However, there are unreadable files dotted throughout the drive that, when accessed, will cause the SATA bus to hang. I've used various resumable file copy applications like robocopy, RichCopy, and a dozen others, but they all have the same issue. They have a RETRY count that is based on actually getting an error reported from the drive. The issue is that the drive is taking an extremely long time to report the error, and this means that a single file may take up to an hour to fail officially. I know how fast each file SHOULD be, so I'd like to build a powershell CMDLET or similar that will allow me to pass in a source and destination file name, and have it try to copy the file. If, after 5 seconds, the file hasn't copied (or if it has - this can be a dumb process), I'd like it to quit. I'll write a script that fires off each copy process individually, waiting for the process before it to finish, but I'm so far unable to find a good way of putting a time limit on the process.

如果您有任何建议,我们将不胜感激!

Any suggestions you might have would be greatly appreciated!

我会很高兴在一个新线程中生成一个 Copy-Item,使用一个新的 PID,然后倒计时,然后终止该 PID.我只是 PowerShell 的新手,并且看到了许多相互冲突的施加计时器的方法,以至于我迷失了最佳实践方式.

I would be happy with spawning a Copy-Item in a new thread, with a new PID, then counting down, then killing that PID. I'm just a novice at PowerShell, and have seen so many conflicting methods for imposing timers that I'm lost on what the best practices way would be.

编辑 2:请注意,robocopy 之类的应用程序在遇到磁盘的坏区时会完全挂起.这些不是简单的挂起,而是总线挂起,Windows 会尝试保留这些挂起,以免丢失数据.在这些情况下,任务管理器无法终止进程,但进程资源管理器是.我不确定方法论的区别是什么,但无论如何,它似乎是相关的.

Edit 2: Please note that applications like robocopy will utterly hang when encountering the bad regions of the disk. These are not simple hangs, but bus hangs that windows will try to preserve in order to not lose data. In these instances task manager is unable to kill the process, but Process Explorer IS. I'm not sure what the difference in methodology is, but regardless, it seems relevant.

推荐答案

我认为在 PowerShell 中执行此类操作的规范方法是 后台工作.

I'd say the canonical way of doing things like this in PowerShell are background jobs.

$timeout = 300 # seconds

$job = Start-Job -ScriptBlock { Copy-Item ... }
Wait-Job -Job $job -Timeout $timeout
Stop-Job -Job $job
Receive-Job -Job $job
Remove-Job -Job $job

用您要运行的任何命令替换脚本块内的 Copy-Item.但请注意,您要在脚本块内使用的所有变量必须在脚本块内定义,通过 -ArgumentList 参数传入,或以 using: 范围为前缀预选赛.

Replace Copy-Item inside the scriptblock with whatever command you want to run. Beware though, that all variables you want to use inside the scriptblock must be either defined inside the scriptblock, passed in via the -ArgumentList parameter, or prefixed with the using: scope qualifier.

Wait-Job 的替代方法是循环,等待作业完成或超时:

An alternative to Wait-Job would be a loop that waits until the job is completed or the timeout is reached:

$timeout = (Get-Date).AddMinutes(5)
do {
  Start-Sleep -Milliseconds 100
} while ($job.State -eq 'Running' -and (Get-Date) -lt $timeout)

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

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