Vijava 学习笔记之 VirtualMachine(状态(通电状态))分析

哈哈 阅读:158 2021-03-31 22:37:53 评论:0
package com.vmware.util; 
 
import com.vmware.vim25.mo.ServiceInstance; 
 
import java.net.URL; 
 
/** 
 * Created by vixuan-008 on 2015/4/17. 
 */ 
public class Session { 
    private static String __serverName = ""; 
    private static String __userName = ""; 
    private static String __passWord = ""; 
    private static String __url = ""; 
 
 
    private static ServiceInstance serviceInstance = null; 
 
 
    public synchronized static ServiceInstance getInstance(String serverName, String userName, String passWord) throws Exception{ 
 
        initServiceInstance(serverName, userName, passWord); 
        return serviceInstance; 
 
    } 
 
 
    public static void initServiceInstance(String ip, String userName, String passWord) throws Exception { 
        __serverName = ip; 
        __userName = userName; 
        __passWord = passWord; 
 
        __url = "https://" + __serverName + "/sdk"; 
         if(serviceInstance==null){ 
             serviceInstance = new ServiceInstance( 
                     new URL(__url), userName, passWord, true); 
         } 
 
 
 
    } 
 
}
<pre name="code" class="html">package com.vmware.client; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.VirtualMachinePowerState; 
import com.vmware.vim25.mo.*; 
 
/** 
 * VirtualMachine 运行状态 
 * Created by vixuan-008 on 2015/4/17. 
 */ 
public class VirtualMachinePowerStateClient { 
    public static void main(String[] args){ 
        try{ 
            //获取 HostSystem 
            ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware"); 
 
            //获取虚拟机运行状态 
            InventoryNavigator inventoryNavigator = null; 
            inventoryNavigator = new InventoryNavigator(serviceInstance.getRootFolder()); 
            ManagedEntity obj = inventoryNavigator.searchManagedEntity("VirtualMachine", "Mssql");//查询指定名称虚拟机 
            if (null == obj){ 
                serviceInstance.getServerConnection().logout(); 
                throw new Exception(); 
            }else{ 
                VirtualMachine virtualMachine=(VirtualMachine)obj; 
                VirtualMachinePowerState state = virtualMachine.getRuntime().powerState;//通电状态 
                if (state.equals(VirtualMachinePowerState.poweredOn)) { 
                   System.out.println("Virtual is ON"); 
                } else if (state.equals(VirtualMachinePowerState.poweredOff) || state.equals(VirtualMachinePowerState.suspended)) { 
                    System.out.println("Virtual is OFF"); 
                } 
            } 
            serviceInstance.getServerConnection().logout(); 
 
        }catch (Exception e){ 
            e.printStackTrace(); 
        } 
    } 
} 

效果展示:
 
<img src="https://img-blog.csdn.net/20150417145040779?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvdXpoaXdlbmdhbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> 
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号