[英]Create Snapshot with WMI in C# with help of the microsoft.management.infrastructure namespace
I'm trying to create a method in C# in which a Snapshot of a Virtual Maschine in Hyper-V is created. 我正在尝试在C#中创建一个在Hyper-V中创建虚拟机快照的方法。
I'm using the new microsoft.management.infrastructure Namespace instead of system.management.instrumentation. 我使用的是新的microsoft.management.infrastructure命名空间,而不是system.management.instrumentation。
My biggest problem is to create the CIM_VirtualSystemSettingData object, which i pass to the invoke method as a reference parameter. 我最大的问题是创建CIM_VirtualSystemSettingData对象,该对象作为参考参数传递给invoke方法。 I dont't know how to set the InstanceId Attribute.
我不知道如何设置InstanceId属性。 Since I always get the error-Message: can't process Targetobject because key-attribute is null (translated from german).
由于我总是收到错误消息:无法处理Targetobject,因为key-attribute为null(从德语翻译)。 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. 我也不确定virtualsystemsetting对象是否是唯一的问题。 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 这是Microsoft对Msvm_VirtualSystemSnapshotService类的WMI方法描述
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. 您将需要创建一个适当类型的空CimInstance,并使用CimInstanceProperties集合的Add方法添加所需的键属性。 Then use the GetInstance method of the CimSession class to retrieve the full CimInstance with all of its data populated.
然后,使用CimSession类的GetInstance方法检索包含其所有数据的完整CimInstance。 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.
为避免自动生成的文档分散您的注意力,我建议您查看目标WMI类实例上的__RELPATH属性。 This will provide the key value pairs.
这将提供键值对。
In PowerShell: 在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: 这意味着在C#中,您需要执行以下操作:
// 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.