I'm trying to create a method in C# in which a Snapshot of a Virtual Maschine in Hyper-V is created.
I'm using the new microsoft.management.infrastructure Namespace instead of system.management.instrumentation.
My biggest problem is to create the CIM_VirtualSystemSettingData object, which i pass to the invoke method as a reference parameter. I dont't know how to set the InstanceId Attribute. Since I always get the error-Message: can't process Targetobject because key-attribute is null (translated from german). When i set it manualy (which i probably shouldn't do anyway) i get an errorcode of:
WBEM_E_INVALID_METHOD_PARAMETERS
2147749935 (0x8004102F)
Parameters provided for the method are not valid.
I'm also not sure if the virtualsystemsetting object is the only problem. But the error message is so vague that i don't know where else to start debugging.
my code so far:
// id is the Virtual machine i want the snapshot to be made of
public String CreateSnapshot(string id) {
string cimNamespace = @"root\virtualization\v2";
string cimMethodName = "CreateSnapshot";
string cimClassName = "Msvm_VirtualSystemSnapshotService";
CimInstance QuellComputer = Session.QueryInstances(@"ROOT\virtualization\v2", "WQL", $"SELECT * FROM CIM_Computersystem").Skip(2).FirstOrDefault();// WHERE Name={id}
CimClass systemSettingClass = Session.GetClass(cimNamespace, "CIM_VirtualSystemSettingData");
CimInstance systemSettingInstance = new CimInstance(systemSettingClass);
systemSettingInstance.CimInstanceProperties["SnapshotDataRoot"].Value = @"C:\Users\SnapshotsTemp";
systemSettingInstance.CimInstanceProperties["ElementName"].Value = @"SnapshotNo1";
systemSettingInstance.CimInstanceProperties["VirtualSystemType"].Value = 5;
CimMethodParametersCollection cimMethodParameters = new CimMethodParametersCollection();
CimMethodParameter cimMethodParameter1 = CimMethodParameter.Create("AffectedSystem", QuellComputer, CimType.Reference, CimFlags.In);
CimMethodParameter cimMethodParameter2 = CimMethodParameter.Create("SnapshotSettings", "", CimType.String, CimFlags.In);
CimMethodParameter cimMethodParameter4 = CimMethodParameter.Create("ResultingSnapshot", generateid, CimType.Reference, CimFlags.In);
CimMethodParameter cimMethodParameter3 = CimMethodParameter.Create("SnapshotType", 2, CimType.UInt16, CimFlags.In);
cimMethodParameters.Add(cimMethodParameter1);
cimMethodParameters.Add(cimMethodParameter4);
cimMethodParameters.Add(cimMethodParameter2);
cimMethodParameters.Add(cimMethodParameter3);
CimMethodResult result = Session.InvokeMethod(cimNamespace, cimClassName, cimMethodName, cimMethodParameters);
}
This is the WMI Method description from Microsoft of the Msvm_VirtualSystemSnapshotService class
uint32 CreateSnapshot(
[in] CIM_ComputerSystem REF AffectedSystem,
[in] string SnapshotSettings,
[in] uint16 SnapshotType,
[in, out] CIM_VirtualSystemSettingData REF ResultingSnapshot,
[out] CIM_ConcreteJob REF Job
Thanks in advance.
You will need to create an empty CimInstance of the appropriate type, and use the Add method of the CimInstanceProperties collection to add the required key properties. Then use the GetInstance method of the CimSession class to retrieve the full CimInstance with all of its data populated. The required key properties are documented online for each class but I have encountered situations where the documentation is incomplete. To avoid allowing auto-generated documentation to distract you from what you can see with your own eyes, I recommend viewing the __RELPATH property on the WMI class instance that you are targeting. This will provide the key value pairs.
In PowerShell:
gwmi -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemSnapshotService | select __relpath | fl
to get...
__RELPATH : Msvm_VirtualSystemSnapshotService.CreationClassName="Msvm_VirtualSystemSnapsho
tService",Name="vssnapsvc",SystemCreationClassName="Msvm_ComputerSystem",Syste
mName="MYHOSTNAME"
Which means in C#, you'll need to do this:
// Using Microsoft.Management.Infrastructure
private const string _namespace = @"root\virtualization\v2";
private const string _hypervHost = "MYHOSTNAME";
using (var cimSession = CimSession.Create(_hyperVHost))
{
// Create generic instance with no instance data
var keyInstance = new CimInstance("Msvm_VirtualSystemSnapshotService");
// Attach key CimProperties
keyInstance.CimInstanceProperties.Add(CimProperty.Create("CreationClassName", "Msvm_VirtualSystemSnapshotService", CimFlags.Key));
keyInstance.CimInstanceProperties.Add(CimProperty.Create("SystemCreationClassName", "Msvm_ComputerSystem", CimFlags.Key));
keyInstance.CimInstanceProperties.Add(CimProperty.Create("Name", "vssnapsvc", CimFlags.Key));
keyInstance.CimInstanceProperties.Add(CimProperty.Create("SystemName", _hyperVHost, CimFlags.Key));
// Retrieve the object
snapShotService = cimSession.GetInstance(_namespace, keyInstance);
}
User contributions licensed under CC BY-SA 3.0