次の方法で共有


Importing VMs Utilizing The Hyper-V WMI v2 Namespace

I have had a few folks ask me about importing VM’s though our WMI namespace.  The Hyper-V WMI v2 interfaces introduce a new concept of planned virtual machines, this  allows you to create/import virtual machines and make configuration changes prior to realizing them or converting them to real virtual machines that can be started or are even visible in the UI.

I have two samples below, the first one simply imports a VM and the second renames the VM as part of the import.  You can get more information about the ImportSystemDefinition API at https://msdn.microsoft.com/en-us/library/hh850082(v=vs.85).aspx.  We also have some additional samples utilizing planned virtual machines in our common sample code at https://msdn.microsoft.com/en-us/library/hh850032(v=vs.85).aspx.

Importing a VM

#Retrieve the management service
$VmMgmtSvc = Get-WmiObject -Namespace root\virtualization\v2 `
      -Class Msvm_VirtualSystemManagementService

#Import the VM, referencing the VM configuration, snapshot folder
$ImportedSystem_ret = $VmMgmtSvc.ImportSystemDefinition(
    "D:\Demo\Virtual Machines\1B3CDA1D-6FB6-4AAE-8E8D-8BE6BB587BD3.XML",
    "D:\Demo\Snapshots",
    $false) #Retaining the VM id

#Retrieve the object referencing the planned VM
$PlannedVM = [WMI]$ImportedSystem_ret.ImportedSystem

#Realize the planned VM
$VmMgmtSvc.RealizePlannedSystem($PlannedVM)

Modifying a VM as part of import

#Retrieve the management service
$VmMgmtSvc = Get-WmiObject -Namespace root\virtualization\v2 `
      -Class Msvm_VirtualSystemManagementService

#Import the VM, referencing the VM configuration, snapshot folder
$ImportedSystem_ret = $VmMgmtSvc.ImportSystemDefinition(
    "D:\Demo\Virtual Machines\1B3CDA1D-6FB6-4AAE-8E8D-8BE6BB587BD3.XML",
    "D:\Demo\Snapshots",
    $false) #Retaining the VM id

#Retrieve the object referencing the planned VM
$PlannedVM = [WMi]$ImportedSystem_ret.ImportedSystem

#Retrieve the setting data for the planned VM
$VmSetData = ($PlannedVM.GetRelated("Msvm_VirtualSystemSettingData", `
      "Msvm_SettingsDefineState", `
      $null, `
      $null, `
      "SettingData", `
      "ManagedElement", `
      $false, $null) | % {$_})

#Modify the name of the VM
$VmSetData.ElementName = "NewVMName"
$VmMgmtSvc.ModifySystemSettings($VmSetData.GetText(2))

#Realize the planned VM
$VmMgmtSvc.RealizePlannedSystem($PlannedVM)

-Taylorb

Comments

  • Anonymous
    March 12, 2014
    is the SystemDefinitionFile xml format documented somewhere?

  • Anonymous
    March 12, 2014
    No, we don't document the VM configuration file (which is what import utilizes).  Historically we change aspects of the configuration file with almost every release so I would strongly caution against taking any dependencies on the file or it's contents. -taylorb

  • Anonymous
    March 14, 2014
    One detail of the import operation is that if you want to restore the original id of the VM the GUID.xml filename cannot be changed.  So importing VMNAME.xml is okay if you want a new id, but it must be the original GUID.xml for the import to succeed.