【问题标题】:Applying a snapshot in Hyper-V WMI V2 from C#从 C# 在 Hyper-V WMI V2 中应用快照
【发布时间】: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,但也没有运气。

我应该改变什么来完成这项工作?

【问题讨论】:

    标签: c# wmi hyper-v


    【解决方案1】:

    我相信您的问题是,当它是出站参数时,您正在传递作业以开始。该参数将从调用中返回。有点像..

    ManagementBaseObject inParams = null;
    
    inParams = PrivateLateBoundObject.GetMethodParameters("ApplySnapshot");
    inParams["Snapshot"] = ((System.Management.ManagementPath)(Snapshot)).Path;
    
    ManagementBaseObject outParams = PrivateLateBoundObject.InvokeMethod("ApplySnapshot", inParams, null);
    
    // i left this as i assume this is vs generated though this isn't how i would normally
    // get my jobs back.
    Job = ((ManagementPath)(outParams.Properties["Job"].Value));
    
    return Convert.ToUInt32(outParams.Properties["ReturnValue"].Value);
    

    【讨论】:

    • 我发现以前的代码中有一个错误,这意味着 PrivateLateBoundObject 没有正确设置,这就是失败的根源。我还与 Microsoft 进行了一些沟通,他们确认了一个 MOF 文件错误 - Job 被标记为 IN OUT 而它应该只是 OUT - 这就是生成的代码是 ref 而不是 out 的原因。最终,虽然这个答案在我的情况下不是 the 答案,但它仍然是足够准确的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多