Note that there are some explanatory texts on larger screens.

plurals
  1. POApplying a snapshot in Hyper-V WMI V2 from C#
    text
    copied!<p>I am trying to replicate the following PowerShell in C#:</p> <pre><code># 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) </code></pre> <p>This code works fine - the snapshot applies as expected.</p> <p>I have no problem getting the <code>Msvm_VirtualSystemSettingData</code> instance or a <code>Msvm_VirtualSystemSnapshostService</code> instance in C#. However, I can't seem to get the call to <code>ApplySnapshot</code> right - no matter what I give it I get an <code>InvalidOperationException</code>. I'm using generated WMI code from Visual Studio for the call:</p> <pre><code>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); } } </code></pre> <p>I'm not sure what to pass for the <code>Job</code> parameter either, since we get a job back - it's very unusual to have a <code>ref</code> for that instead of an <code>out</code> but I've tried a bunch of different variations around that (including setting the parameter to <code>null</code> and not setting it at all) with no luck. I've also tried setting <code>inParams[Job]</code> to <code>null</code> with no luck for that either.</p> <p>What should I change to make this work?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload