通过Wmi实现Hyper-V远程管理(一)
最近公司需要做Hyper-V的远程管理,在现有产品基础上扩展对Hyper V的管理,实现远程开关机、远程开启虚拟机会话,其他内容可查看MSDN中有对Hyper-V的描述和相关实例代码、 Wmi操作hyper的工具类 、hyper-V
ok,废话不说,正文开始...
1.安装hyper-v虚拟机,这里安装的是2012版,安装好之后创建两个虚拟机
2.查阅了好多资料,通用的方式是用Wmi来与hyper进行数据交互,通过工具扫描一下hyper的wmi节点
发现一共有22个直属类
每个类都逐个查看一下,发现能够交互的只有3个大类
逐个调用一下:
1 [dynamic: ToInstance, provider("VmmsWmiInstanceAndMethodProvider"), Locale(1033)] 2 class Msvm_ComputerSystem : CIM_ComputerSystem 3 { 4 [read, ArrayType("Indexed")] uint16 AssignedNumaNodeList[]; 5 [read, Units("MilliSeconds")] uint64 OnTimeInMilliseconds; 6 [read] uint32 ProcessID; 7 [read] datetime TimeOfLastConfigurationChange; 8 [Implemented, ValueMap{"0", "4096", "32768", "32769", "32770", "32771", "32772", "32773", "32774", "32775", "32776", "32777", "32778"}, Bypass_GetObject, ModelCorrespondence{"CIM_EnabledLogicalElement.RequestedState"}] uint32 RequestStateChange([IN, ValueMap{"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "..", "32768..65535"}, ModelCorrespondence{"CIM_EnabledLogicalElement.RequestedState"}] uint16 RequestedState, [OUT] CIM_ConcreteJob ref Job, [IN, SubType("Interval"): ToInstance ToSubClass] datetime TimeoutPeriod); 9 };
其中:
"0", "4096", "32768", "32769", "32770", "32771", "32772", "32773", "32774", "32775", "32776", "32777", "32778"
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "..", "32768..65535"
这些数字分别代表虚拟机实例的各个状态,如:运行,关机,正在关机,等等。如:
0 Unknown 2 Running 3 Off 32768 Paused 32769 Suspended 32770 Starting 32771 Snapshotting 32772 Migrating 32773 Saving 32774 Stopping 32775 Deleted 32776 Pausing 32777 Resuming
保存虚拟机数据的类为 Msvm_ComputerSystem,通过调用Msvm_ComputerSystem可以获取远程hyper下各虚拟机信息,该类包含宿主机的描述:
instance of Msvm_ComputerSystem { AssignedNumaNodeList = NULL; Caption = "宿主计算机系统"; CreationClassName = "Msvm_ComputerSystem"; Dedicated = NULL; Description = "Microsoft 宿主计算机系统"; ElementName = "HYPER-V"; EnabledDefault = 2; EnabledState = 2; HealthState = 5; IdentifyingDescriptions = NULL; InstallDate = NULL; Name = "HYPER-V"; NameFormat = NULL; OnTimeInMilliseconds = NULL; OperationalStatus = {2}; OtherDedicatedDescriptions = NULL; OtherEnabledState = NULL; OtherIdentifyingInfo = NULL; PowerManagementCapabilities = NULL; PrimaryOwnerContact = NULL; PrimaryOwnerName = NULL; ProcessID = NULL; RequestedState = 12; ResetCapability = 1; Roles = NULL; Status = "OK"; StatusDescriptions = {"确定"}; TimeOfLastConfigurationChange = NULL; TimeOfLastStateChange = NULL; };
虚拟机的描述:
instance of Msvm_ComputerSystem { AssignedNumaNodeList = {0}; Caption = "虚拟机"; CreationClassName = "Msvm_ComputerSystem"; Dedicated = NULL; Description = "Microsoft 虚拟机"; ElementName = "windows2012"; EnabledDefault = 2; EnabledState = 2; HealthState = 5; IdentifyingDescriptions = NULL; InstallDate = "20160719031827.000000-000"; Name = "F6F52FD7-42B6-4C2D-BADC-C61F9B5FFD21"; NameFormat = NULL; OnTimeInMilliseconds = "9193908"; OperationalStatus = {2}; OtherDedicatedDescriptions = NULL; OtherEnabledState = NULL; OtherIdentifyingInfo = NULL; PowerManagementCapabilities = NULL; PrimaryOwnerContact = NULL; PrimaryOwnerName = NULL; ProcessID = 4064; RequestedState = 12; ResetCapability = 1; Roles = NULL; Status = "OK"; StatusDescriptions = {"正常运行"}; TimeOfLastConfigurationChange = "20160719084459.532773-000"; TimeOfLastStateChange = "20160719084459.000000-000"; };
instance of Msvm_ComputerSystem { AssignedNumaNodeList = {}; Caption = "虚拟机"; CreationClassName = "Msvm_ComputerSystem"; Dedicated = NULL; Description = "Microsoft 虚拟机"; ElementName = "win2008"; EnabledDefault = 2; EnabledState = 3; HealthState = 5; IdentifyingDescriptions = NULL; InstallDate = "20160719081005.000000-000"; Name = "7A591782-9EEC-46CD-AE43-AFE4EFD23390"; NameFormat = NULL; OnTimeInMilliseconds = "0"; OperationalStatus = {2}; OtherDedicatedDescriptions = NULL; OtherEnabledState = NULL; OtherIdentifyingInfo = NULL; PowerManagementCapabilities = NULL; PrimaryOwnerContact = NULL; PrimaryOwnerName = NULL; ProcessID = NULL; RequestedState = 12; ResetCapability = 1; Roles = NULL; Status = "OK"; StatusDescriptions = {"正常运行"}; TimeOfLastConfigurationChange = "20160719104106.665157-000"; TimeOfLastStateChange = "20160719104106.000000-000"; };
虚拟机信息获取完毕!
本文来自博客园,作者:童心少年,转载请注明原文链接:https://www.cnblogs.com/makeblock/p/5685926.html