2

我的脚本一直困扰着我,但出现以下异常

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

那么,这里出了什么问题?有时有效有时无效...

4

2 回答 2

5

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

于 2013-09-11T09:03:03.163 回答
3

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

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

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

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

于 2013-09-11T08:54:24.080 回答