HyperV WMI Apply Snapshot in c#

0

I want to apply a snapshot with WMI and get an Error called "WS-Management service cannot process the request, unknown error: HRESULT 0x8004102f " This occurs if the parameter set wrong. So I think it has something to do with the input parameter:

uint32 ApplySnapshot(
  [in]  CIM_VirtualSystemSettingData REF Snapshot,
  [out] CIM_ConcreteJob              REF Job
);

Link to ApplySnapshot-description: https://docs.microsoft.com/de-de/windows/win32/hyperv_v2/applysnapshot-msvm-virtualsystemsnapshotservice?redirectedfrom=MSDN#syntax

My code to apply the Snapshot:

public async Task ApplySnapshot(PublicServerSnapshotInfo snapshot, PublicServerInfo publicServer)
        {            
            CimSession session = GetCimSession(publicServer.HostName);
            CimInstance ParamSnapshot = session.QueryInstances(@"root\virtualization\v2", "WQL", "SELECT * From CIM_VirtualSystemSettingData WHERE ConfigurationID='" + snapshot.SnapshotID + "'").FirstOrDefault();

            CimMethodParametersCollection cimMethodParameters = new CimMethodParametersCollection();
            cimMethodParameters.Add(CimMethodParameter.Create("Snapshot", ParamSnapshot,CimType.Reference, CimFlags.In));

            CimMethodResult result = session.InvokeMethod(@"root\virtualization\v2", "Msvm_VirtualSystemSnapshotService", "ApplySnapshot", cimMethodParameters);
        }

I already have some similar code when I want to start and shutdown a virtual machine and it works.

My code to start and stop a virtual machine: (This one works fine)

public async Task RequestStateChange(PublicServerInfo publicServer, string action)
        {
            CimSession session = GetCimSession(publicServer.HostName);
            CimInstance vm = session.QueryInstances(@"root\virtualization\v2", "WQL", "SELECT * FROM Msvm_ComputerSystem WHERE Name='" + publicServer.MachineID + "'").FirstOrDefault();
            CimMethodParametersCollection parameters = new CimMethodParametersCollection();
            if (action.ToLower() == "start")
            {
                parameters.Add(CimMethodParameter.Create("RequestedState", (UInt16)2, CimFlags.In));
            }
            else if (action.ToLower() == "shutdown")
            {
                parameters.Add(CimMethodParameter.Create("RequestedState", (UInt16)4, CimFlags.In));
            }

            CimMethodResult result = session.InvokeMethod(@"root\virtualization\v2", vm, "RequestStateChange", parameters);
        }

Link to RequestsStateChange: https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/requeststatechange-msvm-computersystem

Thanks for every answer

c#
wmi
snapshot
hyper-v
asked on Stack Overflow Feb 11, 2020 by 3r1c

1 Answer

0

I finally found a solution:

public async Task ApplySnapshot(PublicServerSnapshotInfo snapshot, PublicServerInfo publicServer)
        {            
            CimSession session = GetCimSession(publicServer.HostName);
            CimInstance ParamSnapshot = session.QueryInstances(hvNamespace, "WQL", "SELECT * From CIM_VirtualSystemSettingData WHERE ConfigurationID='" + snapshot.SnapshotID + "'").FirstOrDefault();
            var SnapshotService = session.EnumerateInstances(hvNamespace, "Msvm_VirtualSystemSnapshotService").First();

            CimMethodParametersCollection cimMethodParameters = new CimMethodParametersCollection {
                CimMethodParameter.Create("Snapshot", ParamSnapshot, CimType.Reference, CimFlags.In)
            };

            CimMethodResult result = session.InvokeMethod(hvNamespace, SnapshotService, "ApplySnapshot", cimMethodParameters);

        }

This works for me.

answered on Stack Overflow Feb 12, 2020 by 3r1c

User contributions licensed under CC BY-SA 3.0