我正在尝试在C#中创建一个方法,其中创建了Hyper-V中虚拟机的快照。
我正在使用新的microsoft.management.infrastructure命名空间而不是system.management.instrumentation。
我最大的问题是创建CIM_VirtualSystemSettingData对象,我将其作为引用参数传递给invoke方法。我不知道如何设置InstanceId属性。因为我总是得到错误消息:无法处理Targetobject因为key-attribute为null(从德语翻译)。当我设置它manualy(我可能不应该这样做)我得到一个错误代码:
WBEM_E_INVALID_METHOD_PARAMETERS
2147749935 (0x8004102F)
Parameters provided for the method are not valid.
我也不确定virtualsystemsetting对象是否是唯一的问题。但错误信息是如此模糊,以至于我不知道还有什么地方可以开始调试。
我的代码到目前为止:
// 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);
}
这是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
提前致谢。
您需要创建相应类型的空CimInstance,并使用CimInstanceProperties集合的Add方法添加所需的键属性。然后使用CimSession类的GetInstance方法检索填充了所有数据的完整CimInstance。每个类都在线记录了所需的关键属性,但我遇到了文档不完整的情况。为了避免允许自动生成的文档分散您的注意力,我建议您在目标的WMI类实例上查看__RELPATH属性。这将提供键值对。
在PowerShell中:
gwmi -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemSnapshotService | select __relpath | fl
要得到...
__RELPATH : Msvm_VirtualSystemSnapshotService.CreationClassName="Msvm_VirtualSystemSnapsho
tService",Name="vssnapsvc",SystemCreationClassName="Msvm_ComputerSystem",Syste
mName="MYHOSTNAME"
这意味着在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);
}