无法通过Msvm_VirtualSystemSnapshotService在HyperV上应用快照

时间:2017-04-19 09:55:19

标签: wmi hyper-v python-3.6

然后我试试这个:

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','类型不匹配',无,0,-2147217403 ),无)

1 个答案:

答案 0 :(得分: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