卸载图片时出错

时间:2017-06-21 14:28:15

标签: powershell

我在卸载图片时遇到问题:

Dismount-WindowsImage : The specified image is invalid.
Unmount the image or clean up the Vhd and then try again.

前一天,我打破了一些东西,剧本停止了工作。使用脚本导入虚拟机。我挂载图像,将数据添加到图像,卸载并运行。卸载时发生错误:

#PS C:\build-grom-git> .\_start-VM.ps1 -name_of_image "w7" -installer "TGTeDataAVCS+ADPonly" -path_to_vmcx "C:\imageOS\win7x64\w7.vhdx\Virtual Machines\47A23F4B-A8EB-4AAC-A745-4AC28FB66FAE.vmcx"
Param(
    [Parameter(Mandatory=$True,Position=1)]
    [string]$name_of_image,         # path to image of virtual machine                  
    [Parameter(Mandatory=$True,Position=2)]
    [string]$installer,             # name of the folder with install-file
    [Parameter(Mandatory=$True,Position=3)]
    [string]$path_to_vmcx           # path to vmcx file (file for import VM)
)

# static variables
$vhdx_path="Temp\vhdx"              # path to image of virtual machine
$mount_dir="Temp\mount_folder"      # folder of mounting image
# Run As Administrator
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }

# 1. Creating folder
# create temporary folders in local machine
New-Item -ItemType directory -Path $vhdx_path

# 2. Work with vhdx files
#copying image to temporary folder in local machine
Import-VM -Path "$path_to_vmcx" -Copy -VhdDestinationPath "Temp\vhdx" -VirtualMachinePath "Temp\vhdx"

# mounting image
MD $mount_dir -Force
Mount-WindowsImage -ImagePath "$vhdx_path\$name_of_image.vhdx" -Path $mount_dir -Index 1

# copying files in the image (virtual machine)
Copy-Item -Path "C:\keys" -Destination "$mount_dir\keys" -Recurse -force                            # keys for TGTe-Data
Copy-Item -Path "Temp\$installer" -Destination "$mount_dir\Temp\$installer" -Recurse -force         # installer for TGTe-Data
Copy-Item -Path "Temp\DB" -Destination "$mount_dir\DB" -Recurse -force                              # copy database

# unmount image 
Dismount-WindowsImage -Path $mount_dir -Save
RD $mount_dir -Force

#3. Run VM
Start-VM $name_of_image

# enough time to run
Start-Sleep -s 30

如何通过卸载来纠正错误?

1 个答案:

答案 0 :(得分:0)

我无法解决它,但我找到了另一种解决方案。抱歉,我无法正确添加我的代码。 Stack有非常愚蠢的设计。



# mount the vhdx image
$MountedDisk = Mount-VHD -Path "Virtual Hard Disks\windows7x64.vhdx" -Passthru
# get count of the disk in image (i have got 2 disk in image: system-disk and work-disk)
$diskNo=$MountedDisk.DiskNumber
# get letters for this disks
$driveLetter=(Get-Disk $diskNo | Get-Partition).DriveLetter
# Creating path to need disk (i need work-disk)
$path_to_mount_vhdx = $driveLetter[1] + ":"
# Copying files into work-disk
Copy-Item -Path "C:\keys" -Destination "$path_to_mount_vhdx\keys" -Recurse -force	
# unmount 
Dismount-VHD $MountedDisk.Path