Skip Ribbon Commands
Skip to main content

Ondrej Sevecek's English Pages

:

Engineering and troubleshooting by Directory Master!
MCM: Directory

Quick Launch

Ondrej Sevecek's English Pages > Posts > Export Hyper-V virtual machine configuration with PowerShell
August 17
Export Hyper-V virtual machine configuration with PowerShell

Doing Hyper-V virtual machine export of its configuration only (withouth exporting VHD files) with PowerShell is quite simple. It works on Windows 2008, Windows 2008 R2 and Windows 2012 as well and produces the older (Hyper-V 1.0 and Hyper-V 2.0) export format file EXP. So this is an alternative to the new Windows 2012 GUI export method which exports the machine with its XML configuration file only (I have already posted some notes about the new and old export/import formats of virtual machines).

Although the v1 WMI classes (namespace root\virtualization) used in the example have already been deprecated on Windows 2012 and will probably be completelly removed on Windows 2012 R2, with Windows 2012 it still works well.

Note also, although the export file EXP may be similar when you export from Windows 2012 and Windows 2008 R2, you canNOT import newer EXP version into an older system. It means, that if you export virtual machine configuration from a Windows 2012 Hyper-V, you cannot import it into older systems such as Windows 2008 R2. You can import a virtual machine configuration only into the same or newer operating system and Hyper-V version.

Quick notes went inline the script. Just clarify, that the documentation for Msvm_VirtualSystemExportSettingData is incorrect on its CopySnapshotConfiguration member. So if you do not want to export any snapshots, you must specify zero instead of number one, or you get error "Failed to export virtual machine. Can not export a snapshot without state or storage information.".

Correctly, the values of CopySnapshotConfiguration member should be 0 = no snapshots, 1 = one snapshot, 2 = all snapshosts.

$exportDir = 'D:\Export'
$vmName = 'FS1'

$vmms = Get-WmiObject MSVM_VirtualSystemManagementService -Namespace "root\virtualization"
$vm = Get-WmiObject -Query ('SELECT * FROM Msvm_ComputerSystem WHERE ElementName = "{0}"' -f $vmName) -Namespace "root\virtualization"
$es = @($vm.GetRelated('Msvm_VirtualSystemExportSettingData'))[0]  # must retype ManagementObjectCollection to array as it does not define enumerator to be indexed directly

$es.CopySnapshotConfiguration = 0  # no snapshots, the documentation is incorrect in this
$es.CopyVmStorage = $false  # no VHDs
$es.CopyVmRuntimeInformation = $false  # no save state files
$es.CreateVmExportSubdirectory = $true  # yes, create the $vmName folder under $exportDir

$vmms.ExportVirtualSystemEx($vm.Path.Path, $exportDir, $es.GetText(1))

# after this, you will have to wait until the machine is exported
# you can either monitor this with Hyper-V console or implement a more
# complex script to wait until the job finishes.

Comments

Export for Hyper-V 2016

Hello,

If you want a export VM from Hyper-V 2016, you must a change class in WMI to root\virtualization\v2 and you must a set new WMI definition from $vmms.ExportVirtualSystemEx to $vmms.ExportSystemDefinition

New line in script
- $vmms = Get-WmiObject MSVM_VirtualSystemManagementService -Namespace "root\virtualization\v2" #2016

- $vmms.ExportSystemDefinition($vm.Path.Path, $exportDir, $es.GetText(1)) #2016
 on 18/02/2017 10:49

Hyper-V 2016 Comment also Works with 2012R2

EOM
 on 12/05/2017 22:03

Add Comment

Sorry comments are disable due to the constant load of spam *


This simple antispam field seems to work well. Just put here the number.

Title


You do not need to provide any value this column. It will automatically fill with the name of the article itself.

Author *


Body *


Attachments