2017-04-19 91 views
0

然后我试试这个:广东话上的Hyper-V快照申请通过Msvm_VirtualSystemSnapshotService

wmiServerConnection = wmi.WMI(namespace=r'root\virtualization\v2') 
vmSnapManagement = wmiServerConnection.Msvm_VirtualSystemSnapshotService()[0] 
vmSystem = wmiServerConnection.Msvm_ComputerSystem(ElementName=r'wmname')[0] 
vmObjects = vmSystem.associators(wmi_result_class='Msvm_VirtualSystemSettingData') 

for singleVmObject in vmObjects: 
    if singleVmObject.ElementName == r'snapshotname': 
     job = vmSnapManagement.ApplySnapshot(singleVmObject) 

我得到错误: wmi.x_wmi x_wmi:意外的COM错误(-2147352567, '异常发生',(0,' SWbemProperty','Type mismatch',None,0,-2147217403),None)

回答

0

这是VB.Net(不是Python),但希望这会推动你朝正确的方向发展。本示例应用最新快照,但要应用特定快照,只需获取您希望应用的快照的Msvm_VirtualSystemSettingData实例,然后使用该实例代替“lastSnapshot”。

Imports System.Management 

Public Class VirtualSystemSnapshot 
    Public Shared Function Revert(vmElementName As String) 
     Dim scope As New ManagementScope("\\" & ServerName & "\Root\Virtualization\V2", Options) 
     Using virtualMachine As ManagementObject = WmiUtilities.GetVirtualMachine(vmElementName, scope) 
      Using virtualSystemSettingData As ManagementObject = WmiUtilities.GetVirtualSystemSettingData(scope, virtualMachine) 
       Using virtualSystemSnapshotService As ManagementObject = WmiUtilities.GetVirtualSystemSnapshotService(scope) 
        Using lastSnapshot As ManagementObject = WmiUtilities.GetFirstObjectFromCollection(
          virtualSystemSettingData.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_ParentChildSettingData", Nothing, Nothing, Nothing, Nothing, False, Nothing)) 
         Using inParams As ManagementBaseObject = virtualSystemSnapshotService.GetMethodParameters("ApplySnapshot") 
          inParams("Snapshot") = lastSnapshot 

          ' In order to apply a snapshot, the virtual machine must first be saved 
          RequestStateChange.Main(vm, RequestedState.Save) 

          Using outParams As ManagementBaseObject = virtualSystemSnapshotService.InvokeMethod("ApplySnapshot", inParams, Nothing) 
           WmiUtilities.ValidateOutput(outParams, scope) 

           ' Now that the snapshot has been applied, start the VM back up 
           RequestStateChange.Main(vm, RequestedState.Start) 
          End Using 
         End Using 
        End Using 
       End Using 
      End Using 
     End Using 
    End Function 
End Class