• 通过Wmi实现HyperV远程管理(一)


    最近公司需要做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  
    View Code

     


    保存虚拟机数据的类为 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";
    };
    View Code
    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";
    };
    View Code

    虚拟机信息获取完毕!

  • 相关阅读:
    JVM调优-Java中的对象
    Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装
    Navicat for MySQL 64位破解版
    Idea详细配置
    数据库--悲观锁【收藏,待尝试】
    性能问题分析
    java 反射机制之 getDeclaredMethod()获取方法,然后invoke执行实例对应的方法
    Mysql show indexes 查看索引状态
    基于(Redis | Memcache)实现分布式互斥锁
    彻底解决每次打开visio都提示windows正在配置visio的问题
  • 原文地址:https://www.cnblogs.com/makeblock/p/5685926.html
Copyright © 2020-2023  润新知