CreateSnapshot () не создает снимок виртуальной машины hyper-v? - PullRequest
0 голосов
/ 03 декабря 2018
HRESULT hr;
IWbemClassObject *pInClass = NULL;
IWbemClassObject *pOutClass = NULL;

IWbemContext     *pCtx = 0;
IWbemCallResult  *pResult = 0;
BSTR snapshotClassPath = SysAllocString(L"Msvm_VirtualSystemSnapshotService");
hr = m_pWbemServices->GetObject(
    snapshotClassPath, 
    0, 
    NULL, 
    &m_pWbemClassObject, 
    NULL);
if (FAILED(hr))
{
    std::cout << "Failed to get class object(Msvm_VirtualSystemSnapshotService). Error code = 0x" << hex << hr << std::endl;
    return;
}

// Create snapshot method
BSTR MethodNameOne = SysAllocString(L"CreateSnapshot");

hr = m_pWbemClassObject->GetMethod(
    MethodNameOne,
    0,
    &pInClass,
    &pOutClass
);
if (FAILED(hr))
{
    std::cout << "Failed to get method(CreateSnapshot). Error code = 0x" << hex << hr << endl;
}
cout << "Succeeded to get method(CreateSnapshot)." << endl;


BSTR virtualSystemSnaphotSettingData = SysAllocString(L"Msvm_VirtualSystemSnapshotSettingData");
// Get the Msvm_VirtualSystemSnapshotSettingData Class object
IWbemClassObject * pvirtualSystemSnaphotSettingData = NULL;

hr = m_pWbemServices->GetObject(
    virtualSystemSnaphotSettingData,
    0,
    pCtx,
    &pvirtualSystemSnaphotSettingData,
    &pResult
);
if (FAILED(hr))
{
    wprintf(L"Error GetObject Msvm_VirtualSystemSnapshotSettingData:0x%08lx\n", hr);
}
std::cout << "Succeeded to get object(Msvm_VirtualSystemSnapshotSettingData)." << std::endl;

// Create instance of Msvm_VirtualSystemSnapshotSettingData class
IWbemClassObject * pInpInstOfSnapshotSettingData = NULL;
hr = pvirtualSystemSnaphotSettingData->SpawnInstance(
    0,
    &pInpInstOfSnapshotSettingData);
if (FAILED(hr))
{
    wprintf(L"Error SpawnInstance Msvm_VirtualSystemSnapshotSettingData:0x%08lx\n", hr);
}
std::cout << "Succeeded to created instance(Msvm_VirtualSystemSnapshotSettingData )." << std::endl;

// Set the property(Consistency Level)
BSTR memberOneForVirtualSystemSnapshotSettingData = SysAllocString(L"ConsistencyLevel");
VARIANT consistencyLevel;
VariantInit(&consistencyLevel);
V_VT(&consistencyLevel) = VT_BSTR;
V_BSTR(&consistencyLevel) = SysAllocString(L"1");
hr = pInpInstOfSnapshotSettingData->Put(
    memberOneForVirtualSystemSnapshotSettingData,
    0,
    &consistencyLevel,
    0);
if (FAILED(hr))
{
    wprintf(L"Error memberOneForVirtualSystemSnapshotSettingData:0x%08lx\n", hr);
}
wprintf(L"Succeeded to set property(ConsistencyLevel), Consistency level:%s\n", consistencyLevel.bstrVal);
VariantClear(&consistencyLevel);

Метод CreateSnapshot был вызван в Windows Hyper-V 2016, и выполнение прошло успешно, но созданный снимок виртуальной машины не был виден в графическом интерфейсе Hyper-V.

// Установить свойство(ElementName) BSTR memberThreeForVirtualSystemSnapshotSettingData = SysAllocString (L "ElementName");VARIANT elementName;VariantInit (& ElementName);V_VT (& elementName) = VT_BSTR;V_BSTR (& elementName) = SysAllocString (vmName);

hr = pInpInstOfSnapshotSettingData->Put(
    memberThreeForVirtualSystemSnapshotSettingData,
    0,
    &elementName,
    0);
if (FAILED(hr))
{
    wprintf(L"Failed to set property(ElementName), Erorr code:0x%08lx\n", hr);
}
wprintf(L"Succeeded to set property(ElementName):%s\n", elementName.bstrVal);
VariantClear(&elementName);

// Set the property(ignore disks that aren't compatible with snapshots)
BSTR memberTwoForVirtualSystemSnapshotSettingData = SysAllocString(L"IgnoreNonSnapshottableDisks");
VARIANT ignoreNonSnapshottableDisks;
ignoreNonSnapshottableDisks.vt = VT_BOOL;
ignoreNonSnapshottableDisks.boolVal = true;

hr = pInpInstOfSnapshotSettingData->Put(
    memberTwoForVirtualSystemSnapshotSettingData,
    0,
    &ignoreNonSnapshottableDisks,
    0);
if (FAILED(hr))
{
    wprintf(L"Error memberTwoForVirtualSystemSnapshotSettingData:0x%08lx\n", hr);
}
wprintf(L"Succeeded to set property(IgnoreNonSnapshottableDisks):%d\n", ignoreNonSnapshottableDisks.boolVal);
VariantClear(&ignoreNonSnapshottableDisks);

// Create or update instance
hr = m_pWbemServices->PutInstance(
    pInpInstOfSnapshotSettingData,
    0,
    pCtx,
    &pResult);
if (FAILED(hr))
{
    wprintf(L"Error PutInstance:0x%08lx\n", hr);
}
wprintf(L"Succeeded to update the instance.\n");

// Get textual rendering of the object in the MOF syntax
BSTR objString = NULL;
hr = pInpInstOfSnapshotSettingData->GetObjectText(0, &objString);
if (FAILED(hr))
{
    wprintf(L"Error GetObjectText:0x%08lx\n", hr);
}
wprintf(L"Succeeded to get textual rendering.\n");

BSTR ArgNameTwo = SysAllocString(L"SnapshotSettings");
VARIANT v;
V_VT(&v) = VT_BSTR;
V_BSTR(&v) = objString;
wprintf(L"Object text: %s\n", objString);

// Pass the second Argument to CreateSnapshot method
hr = pInClass->Put(ArgNameTwo, 0, &v, 0);
if (FAILED(hr))
{
    wprintf(L"Error ArgNameTwo:0x%08lx\n", hr);
}
VariantClear(&v);
wprintf(L"Succeeded to set property(SnapshotSettings).\n");

// set property(AffectedSystem)
VARIANT vtProp;
m_pWbemClassObject->Get(L"__Path", 0, &vtProp, 0, 0);
hr = pInClass->Put(L"AffectedSystem", 0, &vtProp, NULL);
if (FAILED(hr))
{
    std::cout << "Failed to set property(AffectedSystem)." << std::endl;
}
wprintf(L"AffectedSystem: %s\n", vtProp.bstrVal);
VariantClear(&vtProp);

// Pass the Third Argument to CreateSnapshot method
BSTR ArgNameThree = SysAllocString(L"SnapshotType");
VARIANT var;
VariantInit(&var);
V_VT(&var) = VT_BSTR;
V_BSTR(&var) = SysAllocString(L"32768");
hr = pInClass->Put(ArgNameThree, 0, &var, 0);
if (FAILED(hr))
{
    std::cout << "Failed to set property(SnapshotType)." << std::endl;
}
wprintf(L"SnapshotType: %s\n", var.bstrVal);
VariantClear(&var);

// Call the CreateSnapshot method.
IEnumWbemClassObject* pEnumOb1 = NULL;
hr = m_pWbemServices->ExecQuery(
    BSTR(L"WQL"),
    BSTR(L"SELECT * FROM Msvm_VirtualSystemSnapshotService"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    NULL,
    &pEnumOb1
);
if (FAILED(hr)) {
    std::cout << "SELECT * FROM Msvm_VirtualSystemSnapshotService, Error code:0x%08lx" << hex << hr << endl;
}
std::cout << "Succeeded to query snapshot." << std::endl;

IWbemClassObject *pclsObj1 = NULL;
ULONG uReturn1 = 0;

std::cout << "Create snapshot..." << std::endl;
while (1)
{
    HRESULT hr = pEnumOb1->Next(WBEM_INFINITE, 1, &pclsObj1, &uReturn1);

    if (FAILED(hr) || !uReturn1)
    {
        break;
    }
    VARIANT vt;
    // Get the value of the path property from Msvm_VirtualSystemSnapshotService Class
    hr = pclsObj1->Get(L"__Path", 0, &vt, 0, 0);
    if (FAILED(hr))
    {
        std::wcout << "Failed to get vm's path." << std::endl;
        break;
    }
    wprintf(L"__PATH:%s \n", vt.bstrVal);

    //Call the CreateSnapshot method
    hr = m_pWbemServices->ExecMethod(
        vt.bstrVal,
        MethodNameOne,
        0,
        NULL,
        pInClass,
        &pOutClass,
        NULL);
    if (FAILED(hr))
    {
        wprintf(L"Failed to execute(CreateSnapshot), Erorr code:0x%08lx\n", hr);
        break;
    }
    wprintf(L"Succeeded to create snapshot.\n");
    Sleep(10);

    pclsObj1->Release();
    pclsObj1 = NULL;
}

Возвращаемое значение ExecMethod () равно 0, то есть вызов выполнен успешно, но я не увидел только что созданный снимок, созданный в Hyper-V.

...