简体   繁体   中英

How to get the result after create hyper-v snapshot in wmi

  1. I can not get the result after create hyper-v snapshot on winserver 2012r2, it means I don't know the instanceID of created snapshot.

  2. I've see the method CreateSnapshot of the class Msvm_VirtualSytemSnapshotService , its input params include ResultingSnapshot that reference CIM_VirtualSystemSettingData ,and its output params include ResultingSnapshot too.

  3. I create a CIM_VirtualSystemSettingData instance to invoke the method CreateSnapshot . When I running the program,it tells me

Invalid method Parameter(s)

If I invoke method CreateSnapshot without ResultingSnapshot , I create success.

Please give me some suggestion!

The code:

    ManagementObject virtualSystemService = Utility.GetServiceObject(scope,"Msvm_VirtualSystemSnapshotService");

    ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("CreateSnapshot");

    ManagementPath resultingSettingDataPath = new ManagementPath("CIM_VirtualSystemSettingData");
    ManagementClass serviceClass = new ManagementClass(scope, resultingSettingDataPath, null);
    ManagementObject resultingSettingData = serviceClass.CreateInstance();

    ManagementObject vm = Utility.GetTargetComputer(vmName, scope);

    inParams.SetPropertyValue("AffectedSystem", vm.Path.Path);

    inParams.SetPropertyValue("SnapshotType", snapshotType);

    inParams.SetPropertyValue("ResultingSnapshot", serviceClass.createInstance());

    //inParams.SetPropertyValue("SnapshotSettings", null);
    // inParams.SetPropertyValue("Job", null);
    ManagementBaseObject outParams = virtualSystemService.InvokeMethod("CreateSnapshot", inParams, null);`

You should get the job object and the ResultingSnapshot object from the outParams and not set it as input parameters, After calling the CreateSnapshot method get the Job object and check its' status, if the Job completed then you can get the ResultingSnapshot object from the outParams.

bool snapShotCreated = false;
if ((UInt32)outParams["ReturnValue"] == 4096) // Job in progress
{
    // Get the job object to continue querying
    string jobPath = outParams["job"].ToString();
    ManagementObject job = new ManagementObject(jobPath);

    // TODO: You now have the job object, query it until the job completed.

}
else if ((UInt32)outParams["ReturnValue"] == 0)
{
    // job completed
    snapShotCreated = true;
}
else
{
    // Error 
}

if (snapShotCreated)
{
    string resultingSystemPath = outParams["ResultingSnapshot"].ToString();
    ManagementObject resultingSystem = new ManagementObject(resultingSystemPath);
}

Let me know if it worked...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM