PowerShell:Copy-Item抛出DriveNotFoundException

时间:2013-09-11 08:11:02

标签: exception powershell copy-item

我的脚本一直困扰我以下异常

copy-item : Cannot find drive. A drive with the name 'F' does not exist.
At C:\Program Files (x86)\CA\ARCserve Backup\Templates\RB_Pre_Process.ps1:58 char:1
+ copy-item -Path $drive -Destination $DST_DRIVE -Recurse -ErrorAction Stop
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (F:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

这就是我的脚本的样子。我在驱动器F上安装了一个ISO映像:我添加了一个“start-slepp -s 5”命令,这样我就可以验证图像是否已安装,它可以实现!

$BACKUP_PATH = "E:\00_BACKUP_DATA"
$DR_PATH = "E:\01_DR_DATA"
$ISO_IMAGE = "C:\Program Files (x86)\CA\ARCserve Backup\Templates\Winpe_x64.iso"
$DST_DRIVE = "E:\"

try {
    New-EventLog -LogName Application -Source "RB_Pre_Process.ps1" -ErrorAction Stop
} catch [System.InvalidOperationException] {
    Write-host $_
}

try {
    Write-Host "Preparing RDX cartridge..."

    # Query for disk object 
    $disk_number = (Get-Disk | Where-Object -Property FriendlyName -like "TANDBERG RDX*").Number

    # Remove partitions
    Get-Disk $disk_number | Clear-Disk -RemoveData -Confirm:$false | Out-Null

    # Create new partition
    New-Partition -DiskNumber $disk_number -UseMaximumSize | Out-Null

    # Format partition
    Format-Volume -DriveLetter E -FileSystem NTFS -NewFileSystemLabel "RDX_TAPE" -Confirm:$false  | Out-Null

    # Set partition as active 
    Set-Partition -DriveLetter E -IsActive:$true | Out-Null
} catch {
    Write-Host $_
    Write-EventLog -LogName Application -Source $MyInvocation.MyCommand.Name -EventID 2 -Message $_
}

try {
    Write-Host "Creating folder structure..."

    new-item -itemtype directory -Path $BACKUP_PATH -ErrorAction stop | Out-Null
    new-item -itemtype directory -path $DR_PATH -ErrorAction stop | Out-Null
} catch {
    Write-Host $_
    Write-EventLog -LogName Application -Source $MyInvocation.MyCommand.Name -EventID 2 -Message $_
}

try {
    Write-Host "Mounting ISO image..."

    $image = Mount-DiskImage -ImagePath $ISO_IMAGE -PassThru -ErrorAction Stop
} catch [ParameterBindingException] {
    Write-Host $_
    Write-EventLog -LogName Application -Source $MyInvocation.MyCommand.Name -EventId 2 -Message $_
}

$drive = ($image | Get-Volume).DriveLetter
$drive += ":\*"

Start-Sleep -s 5 

try {
    Write-Host "Copying ISO content..."

    copy-item -Path $drive -Destination $DST_DRIVE -Recurse -ErrorAction Stop
} catch {
    Write-Host $_
    Write-EventLog -LogName Application -Source $MyInvocation.MyCommand.Name -EventId 2 -Message $_
}

try {
    Write-Host "Unmounting ISO image..."

    Dismount-DiskImage -ImagePath $ISO_IMAGE -ErrorAction Stop 
} catch [System.Exception] {
    Write-Host $_ 
    Write-EventLog -LogName Application -Source $MyInvocation.MyCommand.Name -EventId 2 -Message $_
}

那么,这里出了什么问题?有时它有时不起作用......

2 个答案:

答案 0 :(得分:4)

我“解决”了这个问题......当我的脚本直接从PowerShell提示符而不是PowerShell ISE开始时,我的脚本工作得很好......所以IDE是罪魁祸首。

答案 1 :(得分:2)

似乎无法在powershell中访问挂载的图像。我认为这是提供商的限制。可能的解决方法是发出CMD命令。你可以替换

    copy-item -Path $drive -Destination $DST_DRIVE -Recurse -ErrorAction Stop

& cmd /C "copy F:\* G:\dest\"

这里我举一个例子,你可能需要做进一步的工作来递归复制..你可以使用xcopyrobocopy来处理递归复制。