【发布时间】:2013-11-18 22:20:48
【问题描述】:
我正在尝试在 C# 中复制以下 PowerShell:
# Details from here are not particularly important but needed for full sample
$vms = gwmi -namespace "root\virtualization\v2" Msvm_ComputerSystem
$vm = $vms[3]
$snapshot = ($vm.GetRelated("Msvm_VirtualSystemSettingData") | Where { $_.ElementName -eq "SnapshotName" })[0]
# end unimportant
$VMSS = Get-WMiObject -class Msvm_VirtualSystemSnapshotService -namespace root\virtualization\v2
$VMSS.ApplySnapshot($snapshot, $null)
此代码运行良好 - 快照按预期应用。
在 C# 中获取 Msvm_VirtualSystemSettingData 实例或 Msvm_VirtualSystemSnapshostService 实例没有问题。但是,我似乎无法正确拨打ApplySnapshot 的电话——无论我给它什么,我都会得到InvalidOperationException。我正在使用 Visual Studio 生成的 WMI 代码进行调用:
public uint ApplySnapshot(ref System.Management.ManagementPath Job, System.Management.ManagementPath Snapshot) {
if ((isEmbedded == false)) {
System.Management.ManagementBaseObject inParams = null;
inParams = PrivateLateBoundObject.GetMethodParameters("ApplySnapshot");
// following line has been through variations as well with no change -
// commenting it out, setting to null
inParams["Job"] = ((System.Management.ManagementPath)(Job)).Path;
inParams["Snapshot"] = ((System.Management.ManagementPath)(Snapshot)).Path;
System.Management.ManagementBaseObject outParams = PrivateLateBoundObject.InvokeMethod("ApplySnapshot", inParams, null);
Job = ((System.Management.ManagementPath)(outParams.Properties["Job"].Value));
return System.Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
}
else {
return System.Convert.ToUInt32(0);
}
}
我也不确定要为 Job 参数传递什么,因为我们得到了一份工作 - 使用 ref 而不是 out 是非常不寻常的,但我已经尝试了很多围绕它的不同变化(包括将参数设置为null 并且根本不设置它)没有运气。我也尝试将inParams[Job] 设置为null,但也没有运气。
我应该改变什么来完成这项工作?
【问题讨论】: