You are here:   Research
  |  Login

Welcome to my blog, quickest way to find articles is usually to search for them.

Minimize
Search in All Title Contents
 
     

Importing VMs with checkpoints (snapshots) using PowerShell

Apr 18 2017

Yet another late night working with a set of VMs, exported out of one standalone lab environment into another.

Typically I would just use the following PowerShell snippet to import a set of previously exported, or just plain copied, VMs.

# Import WS2012R2 VMs (Configuration 5.0 VMs)
get-childitem "C:\VMS" -recurse -filter *.xml | ForEach-Object {Import-VM -Path $_.FullName}

Or

# Import WS2016 / W10 VMs (Configuration 8.0 VMs)
get-childitem "C:\VMS" -recurse -filter *.vmcx | ForEach-Object {Import-VM -Path $_.FullName}

 

VMs with checkpoints (snapshots)

Now, the preceding command works great when you have VMs without any checkpoints (snapshots), but if you try to run those commands on VM with checkpoint(s), Hyper-V is going to create one VM for each checkpoint, which is typically not what you want :)

Also, more commonly is that you receive “fun” error messages like:

Import-VM : Failed to create virtual machine.
The operation failed because a virtual machine with the same identifier already exists. Select a new identifier and try the operation again.

However, the fix is easy, change your script to only import VMs from the Virtual Machines folder in each VM. Don’t grab the XML or VMCX files in the Snapshots folders.

$AllVirtualMachinesFolders = get-childitem "C:\VMS\" -recurse -filter "Virtual Machines"

Foreach ($VMPath in $AllVirtualMachinesFolders){

    # Import WS2012R2 VMs (Configuration 5.0 VMs)
    Write-Output "Importing VM from folder $VMPath.FullName" 
    get-childitem $VMPath.FullName -recurse -filter *.xml | ForEach-Object {Import-VM -Path $_.FullName}

}

Or

$AllVirtualMachinesFolders = get-childitem "C:\VMS\" -recurse -filter "Virtual Machines"

Foreach ($VMPath in $AllVirtualMachinesFolders){

    # Import WS2016 / W10 VMs (Configuration 8.0 VMs)
    Write-Output "Importing VM from folder $VMPath.FullName" 
    get-childitem $VMPath.FullName -recurse -filter *.vmcx | ForEach-Object {Import-VM -Path $_.FullName}

}

Written by Johan Arwidmark








Happy deployment, and thanks for reading!
/ The Deployment Research team



Ami Casto

Johan Arwidmark

Blog Archive

Minimize