簡體   English   中英

通過C#在Hyper-V WMI V2中應用快照

[英]Applying a snapshot in Hyper-V WMI V2 from C#

我正在嘗試在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正確調用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是很不尋常的out但是我已經嘗試了很多不同的變化(包括設置參數設置為null並且完全沒有設置)。 我也曾嘗試將inParams[Job]設置為null ,但也沒有運氣。

為了使這項工作有效,我應該更改什么?

我相信您的問題是,當它是出站參數時,您正在傳遞作業。 該參數將從調用中返回。 就像是..

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM