简体   繁体   English

使用 PowerShell 中的 WinSCP 将整个驱动器上传到服务器,跳过所有无法读取的文件和文件夹

[英]Upload whole drive to a server using WinSCP in PowerShell skipping all files and folders that cannot be read

I'm trying to copy an entire drive for example D: drive files from a Windows box to a Linux box using PowerShell and WinSCPnet.dll . I'm trying to copy an entire drive for example D: drive files from a Windows box to a Linux box using PowerShell and WinSCPnet.dll . When I try copying particular folders, I am able to copy all the files from that folder to the Linux path.当我尝试复制特定文件夹时,我可以将该文件夹中的所有文件复制到 Linux 路径。 But when I try to copying the entire D: drive to a Linux box folder, I am getting the following error.但是当我尝试将整个 D: 驱动器复制到 Linux 盒子文件夹时,我收到以下错误。

Error retrieving file list for "d:\$RECYCLE.BIN\S-1-5-21-1458064458-1966517317-3155185246-1003*.*".检索“d:\$RECYCLE.BIN\S-1-5-21-1458064458-1966517317-3155185246-1003*.*”的文件列表时出错。 System Error.系统错误。 Code: 5.代码:5。

$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles("d:\*", "/tmp/some/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()

Expected result should be that it should copy the entire D drive to the destination Linux box folder, but the above mentioned error is coming.预期的结果应该是它应该将整个D盘复制到目标Linux文件夹,但是上面提到的错误来了。

You can make WinSCP .NET assembly skip all files and folders that cannot be opened by handlingSession.QueryReceived event :您可以通过处理Session.QueryReceived事件使 WinSCP .NET 程序集跳过所有无法打开的文件和文件夹:

# Continue on any error
$session.add_QueryReceived( { 
    Write-Host "Error: $($_.Message)"
    $_.Continue()
} )

$session.PutFiles("d:\*", "/tmp/some/").Check()

Code based on article Recursively download directory tree with custom error handling .基于文章Recursively download directory tree with custom error handling的代码。


Another option is to explicitly exclude the problematic files and folders from the transfer using TransferOptions.FileMask .另一种选择是使用TransferOptions.FileMask从传输中明确排除有问题的文件和文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM