RecoverVMUsingRapidProvisioning.ps1

Applies To: Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

You can use rapid provisioning to recover a virtual machine after a Hyper-V host fails and cannot be recovered. For an import to work, Hyper-V requires an exported virtual machine configuration. However, if your host fails, and you do not have the exported virtual machine configuration, you need to create a new virtual machine from the local virtual hard disk file. To recover the virtual machine, you must know the absolute path of the virtual hard disk file on the host. If you used Hyper-V to create a snapshot of the virtual machine, you can get the latest .avhd file, as determined by its time stamp, and use it to create the new virtual machine.

To recover a virtual machine from an .avhd file using rapid provisioning

  1. Get the full Universal Naming Convention (UNC) path to the .avhd file by running the following commands at the command prompt. Make sure you replace the sample UNC path with your own.

    $AVHD = get-childitem \\VMMHost01\d$\VirtualMachinePath\VM01 -Filter "*.avhd" -Recurse | sort-object @{Expression={$_.LastWriteTime-$_.CreationTime}; Ascending=$true}
    $AVHDPath = $AVHD[0].DirectoryName
    Write-Host $AVHDPath
    

    Use the returned UNC path to determine the local path of the .avhd file. For example, if the returned UNC path is \\VMMHost01\d$\VirtualMachinePath\VM01, the local path is D:\VirtualMachinePath\VM01. Use this local path in steps 4 and 5.

  2. Get a virtual hard drive file from the library (this can be any .vhd file because it is a placeholder).

  3. Get the host to which you want to recover the virtual machine.

  4. Specify the .avhd file in the New-VirtualDiskDrive cmdlet by using the following parameters:

    • VirtualHardDisk: Supply the placeholder .vhd file.

    • Path: Supply the local .avhd file path derived from the UNC path that you obtained in step 1. For example, D:\VirtualMachinePath\VM01 or D:\VirtualMachinePath\VM01\Snapshots\<GUID>.

    • FileName: Supply the .avhd file name.

  5. Create the virtual machine by using the New-VM cmdlet with the following parameters:

    • Path: Supply the local path derived from the UNC path that you obtained in step 1. This does not need to be the full path of the .avhd file because VMM will create a new folder for the virtual machine. For example, D:\VirtualMachinePath\VM01.

    • UseLocalVirtualHardDisks: Makes the job use the .avhd file you specified in New-VirtualDiskDrive.

    • SkipInstallVirtualizationGuestServices: Skips the installation of Integration Components if they are already installed.

    • RunAsynchronously: Immediately returns command to the command window.

Disclaimer

# Filename:      RecoverVMUsingRapidProvisioning.ps1
# Description:   Recovers a virtual machine from an .avhd file.

# Connect to the Virtual Machine Manager (VMM) server.
$VMMServer = Get-VMMServer -ComputerName "VMMServer01.contoso.com"

# Get the latest .avhd file for the virtual machine.
$AVHD = get-childitem \\VMMHost01\d$\VirtualMachinePath\VM01 -Filter "*.avhd" -Recurse | sort-object @{Expression={$_.LastWriteTime-$_.CreationTime}; Ascending=$true}

# The first .avhd file in the list should be the latest one. 
# From the object, you can get the location of the .avhd file
# and the file name.
$AVHDPath = $AVHD[0].DirectoryName
$AVHDName = $AVHD[0].Name

Write-Host $AVHDPath
Write-Host $AVHDName

# Create a new virtual machine from the existing .avhd file.
$JobGroupID = [Guid]::NewGuid().ToString()
$VMHost = Get-VMHost | where {$_.ComputerName -eq "VMMHost01"}
$VHD = @(Get-VirtualHardDisk | where {$_.Name -match "Large"})
New-VirtualDiskDrive -VirtualHardDisk $VHD[0] -IDE -BUS 0 –LUN 0 -Path D:\VirtualMachinePath\VM01 -Filename $AVHDName -JobGroup $JobGroupID
New-VM -VMHost $VMHost -Name "SnapshotRecover_01" -Path D:\VirtualMachinePath\VM01 -JobGroup $JobGroupID -UseLocalVirtualHardDisks -Description "" -Owner "Contoso\Phyllis" -CPUCount 1 -MemoryMB 512 -VMMServer $VMMServer –SkipInstallVirtualizationGuestServices -RunAsynchronously -JobVariable "NewVMJob"

#Display the job progress.
While ($NewVMJob.status -eq "Running")
{
   Write-Progress -activity "Creating the new virtual machine" -status $NewVMJob.CurrentStep
   Start-Sleep -seconds 5
}