修改ManagementObjectCollection以接收Hyper-V中的所有检查点

时间:2018-06-13 14:18:37

标签: c# wmi

我正在编写一些与Hyper-V VM配合使用的程序。 我有一个函数,它应该获取vm的所有检查点。 如果用户想要回滚到特定的检查点,他会设置名称,程序应该回滚。 遵循我的功能。但问题是它只找到当前的检查点,并没有显示所有检查点。您能否建议我应该更改哪些内容以获取所有检查点名称?

public IEnumerable<ManagementObject> GetVirtualMachines()
{
  return GetObjects("Msvm_ComputerSystem").Where(x => "Virtual Machine" == 
  (string)x["Caption"]);
}

public ManagementObject GetObject(string serviceName)
{
  return GetObjects(serviceName).FirstOrDefault();
}


void SomeFunction(string name of VMName)
{

  ManagementObject computer = GetVirtualMachines().First(vm => vm["ElementName"] as string == VMName);
  var snapshotService = GetObject("Msvm_VirtualSystemSnapshotService");

   using (ManagementBaseObject inParams = snapshotService.GetMethodParameters("ApplySnapshot"))
  {
    ManagementObject setting = null;

    ManagementObjectCollection settings = computer.GetRelated(
        "Msvm_VirtualSystemSettingData",
        "Msvm_MostCurrentSnapshotInBranch",
        null,
        null,
        "Dependent",
        "Antecedent",
        false,
        null
        );

    foreach (ManagementObject instance in settings)
    {
        string NameOfCheckpoint= setting.GetText(???);//How to get all checkpoints?
        string Parent=setting.GetText(???);//How get parents checkpoints?
        Console.WriteLine("Check point name {0}. Parent: {1}",NameOfCheckpoint,Parent);
        if(NameOfCheckpoint=="specify name")//If name of checkpoint selected, appply rollback to checkpoint
        {
            var snapshotService = scope.GetObject("Msvm_VirtualSystemSnapshotService");

            var inParameters = snapshotService.GetMethodParameters("ApplySnapshot");
            inParameters["Snapshot"] = instance.Path.Path;
            var outParameters = snapshotService.InvokeMethod("ApplySnapshot", inParameters, null);
            Console.WriteLine((uint)outParameters["ReturnValue"]);
        }
    }

  }
}

0 个答案:

没有答案