windows server 2016
我通過wmi呼叫CreateSnapshot,準備生成快照,介面呼叫回傳0,呼叫成功,卻并沒有產生快照,以下是代碼
BOOL CWMIUtil::CreateSnapshot(wchar_t* ElementName, wchar_t* strName)
{
HRESULT hr;
IWbemClassObject* pVirtualSysSnapshotService = NULL;
IWbemClassObject* pVirutalSysMgrService = NULL;
IWbemContext *pCtx = 0;
IWbemCallResult *pResult = 0;
//檢索虛擬機快照實體
BSTR snapshotClassPath = SysAllocString(L"Msvm_VirtualSystemSnapshotService");
BSTR VirtualSysMgrService = SysAllocString(L"Msvm_VirtualSystemManagementService");
do {
//獲取虛擬機管理實體物件
hr = m_pSvc->GetObject(
VirtualSysMgrService,
0,
NULL,
&pVirutalSysMgrService,
NULL);
//獲取實體物件失敗
if (FAILED(hr)) {
std::cout << "Failed to get class object(Msvm_VirtualSystemManagementService). Error code = 0x" << hex << hr << std::endl;
wprintf(L"Failed to get class object(Msvm_VirtualSystemManagementService). Error code = 0x\n");
break;
}
std::cout << "Succeeded to get object(Msvm_VirtualSystemManagementService)." << std::endl;
//獲取系統快照實體物件
hr = m_pSvc->GetObject(
snapshotClassPath,
0,
NULL,
&pVirtualSysSnapshotService,
NULL);
//獲取實體物件失敗
if (FAILED(hr)) {
std::cout << "Failed to get class object(Msvm_VirtualSystemSnapshotService). Error code = 0x" << hex << hr << std::endl;
wprintf(L"Failed to get class object(Msvm_VirtualSystemSnapshotService). Error code = 0x\n");
break;
}
// Call the GetTargetComputer
IEnumWbemClassObject* pComputer = NULL;
wchar_t comsystem[100] = L"SELECT * FROM Msvm_ComputerSystem ";
hr = m_pSvc->ExecQuery(
BSTR(L"WQL"),
BSTR(comsystem),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pComputer
);
if (FAILED(hr)) {
std::cout << "SELECT * FROM msvm_computersystem, Error code:0x%08lx" << hex << hr << endl;
break;
}
std::cout << "Succeeded to query pComputer." << std::endl;
IWbemClassObject *pObjectVirtualMachine = NULL;
ULONG pReturn1 = 0;
wprintf(L"ElementName:%s \n", strName);
while (!FAILED(pComputer->Next(WBEM_INFINITE, 1, &pObjectVirtualMachine, &pReturn1))) {
VARIANT vt;
// Get the value of the path property from Msvm_VirtualSystemSnapshotService Class
hr = pObjectVirtualMachine->Get(L"ElementName", 0, &vt, 0, 0);
if (FAILED(hr)) {
std::wcout << "Failed to get vm's path." << std::endl;
}
wprintf(L"ElementName:%s \n", vt.bstrVal);
CString csValue;
switch (vt.vt)
{
case VT_BSTR:
csValue = CString(CStringW(vt.bstrVal));
break;
default:
break;
}
if (wcscmp(strName, csValue) == 0) {
break;;
}
pObjectVirtualMachine = NULL;
}
if (pObjectVirtualMachine == NULL) {
printf("沒有找到要創建快照的虛擬機\n");
break;
}
//獲取Msvm_VirtualSystemSnapshotSettingData類物件
IWbemClassObject * pVirtualSystemSnaphotSettingData = NULL;
hr = m_pSvc->GetObject(L"Msvm_VirtualSystemSnapshotSettingData", 0, NULL, &pVirtualSystemSnaphotSettingData, &pResult);
//創建Msvm_VirtualSystemSnapshotSettingData類的實體
IWbemClassObject * pInpInstOfSnapshotSettingData = NULL;
hr = pVirtualSystemSnaphotSettingData->SpawnInstance(
0,
&pInpInstOfSnapshotSettingData);
if (FAILED(hr)) {
wprintf(L"Error SpawnInstance Msvm_VirtualSystemSnapshotSettingData:0x%08lx\n", hr);
break;
}
std::cout << "Succeeded to created instance(Msvm_VirtualSystemSnapshotSettingData )." << std::endl;
//檢索將被快照的虛擬機計算機系統的實體
VARIANT ConsistencyLevel;
VariantInit(&ConsistencyLevel);
V_VT(&ConsistencyLevel) = VT_BSTR;
V_BSTR(&ConsistencyLevel) = SysAllocString(L"2");
hr = pInpInstOfSnapshotSettingData->Put(L"ConsistencyLevel", 0, &ConsistencyLevel, CIM_UINT8);
if (FAILED(hr)) {
wprintf(L"Failed to set property(ConsistencyLevel), Erorr code:0x%08lx\n", hr);
break;
}
wprintf(L"Succeeded to set property(ConsistencyLevel)\n");
VariantClear(&ConsistencyLevel);
/*
VARIANT IgnoreNonSnapshottableDisks;
V_VT(&IgnoreNonSnapshottableDisks) = VT_BOOL;
V_UI8(&IgnoreNonSnapshottableDisks) = true;
hr = pInpInstOfSnapshotSettingData->Put(L"IgnoreNonSnapshottableDisks", 0, &IgnoreNonSnapshottableDisks, CIM_BOOLEAN);
if (FAILED(hr)) {
wprintf(L"Failed to set property(IgnoreNonSnapshottableDisks), Erorr code:0x%08lx\n", hr);
break;
}
VariantClear(&IgnoreNonSnapshottableDisks);
*/
VARIANT ElementName;
V_VT(&ElementName) = VT_BSTR;
V_BSTR(&ElementName) = SysAllocString(L"vm1");
hr = pInpInstOfSnapshotSettingData->Put(L"ElementName", 0, &ElementName, 0);
if (FAILED(hr)) {
wprintf(L"Failed to set property(IgnoreNonSnapshottableDisks), Erorr code:0x%08lx\n", hr);
break;
}
wprintf(L"Succeeded to set property(IgnoreNonSnapshottableDisks)\n");
wprintf(L"Succeeded to set property(IgnoreNonSnapshottableDisks)11111\n");
// 創建快照方法
BSTR MethodNameOne = SysAllocString(L"CreateSnapshot");
IWbemClassObject *pCreateSnapShotParamClass = NULL;
hr = pVirtualSysSnapshotService->GetMethod(
MethodNameOne,
0,
&pCreateSnapShotParamClass,
NULL);
if (FAILED(hr)) {
std::cout << "Failed to get method(CreateSnapshot). Error code = 0x" << hex << hr << endl;
break;
}
cout << "Succeeded to get method(CreateSnapshot)." << endl;
IWbemClassObject *pCreateSnapShotParamInstance = NULL;
hr = pCreateSnapShotParamClass->SpawnInstance(0, &pCreateSnapShotParamInstance);
if (FAILED(hr)) {
std::cout << "SpawnInstance, pCreateSnapShotParamInstance Error code:0x%08lx" << hex << hr << endl;
break;
}
std::cout << "Succeeded to SpawnInstance pCreateSnapShotParamInstance." << std::endl;
// Create or update instance
hr = m_pSvc->PutInstance(
pInpInstOfSnapshotSettingData,
0,
pCtx,
&pResult);
if (FAILED(hr))
{
wprintf(L"Error PutInstance:0x%08lx\n", hr);
}
// 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);
break;
}
wprintf(L"Succeeded to get textual rendering.\n");
BSTR ArgSnapshotSettings = 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 = pCreateSnapShotParamInstance->Put(ArgSnapshotSettings, 0, &v, 0);
if (FAILED(hr)) {
wprintf(L"Error ArgNameTwo:0x%08lx\n", hr);
break;
}
VariantClear(&v);
wprintf(L"Succeeded to set property(SnapshotSettings).\n");
// 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 = pCreateSnapShotParamInstance->Put(ArgNameThree, 0, &var, CIM_UINT16);
if (FAILED(hr)) {
wprintf(L"fail SnapshotType error code: 0x%08lx\n", hr);
break;
}
VariantClear(&var);
VARIANT AffectedSystem;
pObjectVirtualMachine->Get(L"__Path", 0, &AffectedSystem, 0, 0);
wprintf(L"MachineName:%s\n", AffectedSystem.bstrVal);
hr = pCreateSnapShotParamInstance->Put(L"AffectedSystem", 0, &AffectedSystem, 0);
if (FAILED(hr)) {
wprintf(L"fail AffectedSystem error code: 0x%08lx\n", hr);
break;
}
VariantClear(&AffectedSystem);
IWbemClassObject* pVirtualSysSnapshotService1 = NULL;
hr = GetSnapshotSeviceObject(m_pSvc, &pVirtualSysSnapshotService1);
if (FAILED(hr)) {
wprintf(L"GetSnapshotSeviceObject faied: 0x%08lx\n", hr);
break;
}
VARIANT method_path;
pVirtualSysSnapshotService1->Get(L"__Path", 0, &method_path, 0, 0);
wprintf(L"MethodPath:%s\n", method_path.bstrVal);
CComPtr<IWbemClassObject> pOutParam = NULL;
IWbemCallResult * pCallResult = NULL;
hr = m_pSvc->ExecMethod(
method_path.bstrVal,
L"CreateSnapshot",
0,
NULL,
pCreateSnapShotParamInstance,
&pOutParam,
&pCallResult);
if (FAILED(hr)) {
wprintf(L"Failed to execute(CreateSnapshot), Erorr code:0x%08lx\n", hr);
break;
}
wprintf(L"Create SnapShot Success!\n");
long result = 0;
while (WBEM_S_NO_ERROR != pCallResult->GetCallStatus(30000, &result)) {
wprintf(L".");
}
wprintf(L"Result = %08x\n", result);
IWbemClassObject *pResObj = NULL;
hr = pCallResult->GetResultObject(0, &pResObj);
CComVariant jobPath;
if (FAILED(pOutParam->Get(L"Job", 0, &jobPath, NULL, NULL))) {
wprintf(L"Get JOB failed!\n");
break;
}
CComPtr<IWbemClassObject> pResult;
hr = GetRelated(jobPath.bstrVal, L"Msvm_VirtualSystemReferencePoint", &pResult);
if (FAILED(hr)) {
wprintf(L"GetRelated failed!\n");
}
}while (false);
return true;
}
執行之后

創建快照介面成功,但是磁盤目錄并沒有生成快照檔案

但是用power-shell腳本生成快照,是會生成快照檔案的

有人能解答我的疑問嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/284693.html