USB中的复制项目无法工作

时间:2019-06-20 15:32:28

标签: powershell copy-item powershell-jobs

我正在使用USB的路径(假设已插入一个驱动器)而不是驱动器号将文件复制到USB。我需要这样做,因为我要用它一次克隆52个USB。

在此处找到路径的前缀:

$usb = Get-Volume | where {$_.DriveType -eq "Removable" }
$usb.Path

与其余文件名串联在一起时,会变成类似以下内容:

\\?\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\RDC_2.1.1_ALL.dmg

当我在工作以外致电Copy-Item时,一切正常,并可以正确复制。当我在PowerShell作业中调用Copy-Item时,它失败并抱怨路径。这对我来说毫无意义。

有人可以告诉我为什么我的功能copyFilesSync正常工作,而copyFiles不起作用吗?

为简单起见,我已将脚本切回到仅能重现此问题的元素。 $filestoCopyJSON1$filestoCopyJSON2之间没有实际区别。交换实际文件没有任何区别。我只需要两个文件名,这样我就可以背靠背运行这两个函数,而它们并没有复制同一文件:

$filestoCopyJSON1 = '[
    {
        "FromFile":  "C:\\dev\\VMware-player-12.1.1-3770994.exe",
        "ToFile":  "\\\\?\\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\\VMware-player-12.1.1-3770994.exe"
    }
]'

$filestoCopyJSON2 = '[
    {
        "FromFile":  "C:\\dev\\RDC_2.1.1_ALL.dmg",
        "ToFile":  "\\\\?\\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\\RDC_2.1.1_ALL.dmg"
    }
]'

function copyFiles($filestoCopy){
    Write-Host "Copying $($filestoCopy.Count)"
    $copyblock = {
        $copy = $args[0]
        Write-Host "$($copy.FromFile)" "$($copy.ToFile)"
        Copy-Item "$($copy.FromFile)" "$($copy.ToFile)"
        $resultHash = Get-FileHash -LiteralPath "$($copy.ToFile)"
    }

    foreach ($filetoCopy in $filestoCopy) {
        Start-Job -ScriptBlock $copyblock -ArgumentList @($filetoCopy)
    }

    Write-Host "Waiting on copying..."
    Get-Job | Wait-Job
    Write-Host "Files Copied"
}

function copyFilesSync($filestoCopy){
    foreach ($filetoCopy in $filestoCopy) {
        Copy-Item "$($filetoCopy.FromFile)" "$($filetoCopy.ToFile)"
        $resultHash = Get-FileHash -LiteralPath "$($filetoCopy.ToFile)"
    }
}

Remove-Job -Name *
Write-Host "Sync File Copy"

copyFilesSync -filestoCopy $(ConvertFrom-JSON -InputObject $filestoCopyJSON1)

Write-Host "Async File Copy"
copyFiles -filestoCopy $(ConvertFrom-JSON -InputObject $filestoCopyJSON2)
Receive-Job -Name *

这将产生以下输出:

PS C:\WINDOWS\system32> C:\dev\Test.ps1
Sync File Copy
Async File Copy
Copying 1

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
207    Job207          BackgroundJob   Running       True            localhost            ...
Waiting on copying...
207    Job207          BackgroundJob   Completed     True            localhost            ...
Files Copied
C:\dev\RDC_2.1.1_ALL.dmg \\?\Volume{df4d4042-9133-11e9-9a6b-48a472473e67}\RDC_2.1.1_ALL.dmg
Illegal characters in path.
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
    + PSComputerName        : localhost

Copy-Item中第一个不在作业中的文件已成功完成。通过作业运行的异步Copy-Item失败,并显示错误。

1 个答案:

答案 0 :(得分:0)

我相信这是Powershell Jobs或Copy-Item中的错误。我不太确定是哪一个。我能够通过使用后台运行空间而不是乔布斯来绕开它。因此,以下异步代码可以正常运行:

     function copyFiles($filestoCopy){
        Write-Host "Copying $($filestoCopy.Count)" 
        $copyblock = {
            $filestoCopy = $args[0]
            foreach($filetoCopy in $filestoCopy){
                Copy-Item "$($filetoCopy.FromFile)" "$($filetoCopy.ToFile)"
                $resultHash = Get-FileHash -LiteralPath "$($filetoCopy.ToFile)"

                if($filetoCopy.FromFileHash -ne $resultHash.hash){
                    Write-Host "File $($copy.ToFile) Failed.. trying again" 
                    Copy-Item "$($filetoCopy.FromFile)" "$($filetoCopy.ToFile)"
                }    
            }
        }

        $newPowerShell = [PowerShell]::Create().AddScript($copyblock).AddArgument($filestoCopy)
        $job = $newPowerShell.BeginInvoke()


        Write-Host "Waiting on copying..." 
        While (-Not $job.IsCompleted) {}
        $result = $newPowerShell.EndInvoke($job)
        $newPowerShell.Dispose()
        Write-Host "Files Copied" 
    }