Pergunta

My script keeps bugging me with the following exception

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

This is what my script looks like. I am mounting an ISO image on drive F: and I have added a "start-slepp -s 5" command so i can verify the image get's mounted, which it does!

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

So, what's going wrong here? Sometimes it works sometimes not...

Foi útil?

Solução

I "solved" the issue... my script is working perfectly fine when it's getting started directly from the PowerShell prompt instead of the PowerShell ISE... So the IDE is the culprit.

Outras dicas

it seems the mounted image can't be reached in powershell. I think it's a limitation of the provider. A possible workaround is issuing CMD command. You could replace

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

with

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

Here I just give an example, you may need to do further work to copy recursively..you could use the xcopy or robocopy which could handle recursive copy.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top