HyperV WMI 在 c# 中应用快照
HyperV WMI Apply Snapshot in c#
我想使用 WMI 应用快照并收到名为 "WS-Management service cannot process the request, unknown error: HRESULT 0x8004102f " 的错误
如果参数设置错误,就会出现这种情况。
所以我觉得跟入参有关系:
uint32 ApplySnapshot(
[in] CIM_VirtualSystemSettingData REF Snapshot,
[out] CIM_ConcreteJob REF Job
);
Link 到 ApplySnapshot-描述:https://docs.microsoft.com/de-de/windows/win32/hyperv_v2/applysnapshot-msvm-virtualsystemsnapshotservice?redirectedfrom=MSDN#syntax
我应用快照的代码:
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);
}
当我想要启动和关闭虚拟机时,我已经有一些类似的代码并且它可以工作。
我的启动和停止虚拟机的代码:(这个工作正常)
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 到 RequestsStateChange:https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/requeststatechange-msvm-computersystem
感谢您的每一个回答
我终于找到了解决方案:
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);
}
这对我有用。
我想使用 WMI 应用快照并收到名为 "WS-Management service cannot process the request, unknown error: HRESULT 0x8004102f " 的错误 如果参数设置错误,就会出现这种情况。 所以我觉得跟入参有关系:
uint32 ApplySnapshot(
[in] CIM_VirtualSystemSettingData REF Snapshot,
[out] CIM_ConcreteJob REF Job
);
Link 到 ApplySnapshot-描述:https://docs.microsoft.com/de-de/windows/win32/hyperv_v2/applysnapshot-msvm-virtualsystemsnapshotservice?redirectedfrom=MSDN#syntax
我应用快照的代码:
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);
}
当我想要启动和关闭虚拟机时,我已经有一些类似的代码并且它可以工作。
我的启动和停止虚拟机的代码:(这个工作正常)
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 到 RequestsStateChange:https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/requeststatechange-msvm-computersystem
感谢您的每一个回答
我终于找到了解决方案:
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);
}
这对我有用。