Posted by : 小p
04 6月 2013
連接上ESX Server後,接下來就可以對ESX Server裡面的VM做一些操作拉~
首先,先開個機,暖暖身吧~~~<誤>
在Power on VM的部分,如果是連到vCenter 進行操作,則需要該VM的
DataCenter Name,Host Name,以及要power on的VM Name
程式碼範例如下
其中如何與Esx、vCenter連線的部分,請參考
[VMware] 使用JAVA撰寫vim --建立與Esx Server的連線
public void powerOnVM(String dcname, String hostname, String VMname) throws Exception { rootFolder = si.getRootFolder(); HostSystem host = null; String apiType = si.getAboutInfo().getApiType(); if ("HostAgent".equalsIgnoreCase(apiType) && dcname == null) { dcname = "ha-datacenter"; } else if ("VirtualCenter".equalsIgnoreCase(apiType) && ((dcname == null) || (hostname == null))) { System.out.println("datacenter and host should be specified"); return; } atacenter dc = (Datacenter) new InventoryNavigator(rootFolder) .searchManagedEntity("Datacenter", dcname); host = (HostSystem) new InventoryNavigator(dc).searchManagedEntity( "HostSystem", hostname); vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()) .searchManagedEntity("VirtualMachine", VMname); try { Task task = vm.powerOnVM_Task(null); task.waitForTask(); if (task.getTaskInfo().getState() == TaskInfoState.error) { System.out.println("Failure: Virtual Machine cannot PowerOn"); System.out.println(task.getTaskInfo().getState().toString()); } if (task.getTaskInfo().getState() == TaskInfoState.success) { System.out.println("Virtual Machine PowerOn successfully."); } } catch (Exception e) { System.out.println("Exception while power on: " + e); } }說明:
rootFolder = si.getRootFolder();si為已連接至Esx(vCenter)之ServiceInstance object
String apiType = si.getAboutInfo().getApiType();確認si連線對象是vCenter還是Esx Server
if ("HostAgent".equalsIgnoreCase(apiType) && dcname == null) {dcname = "ha-datacenter";}如果連線對象是,Esx Server,將dcnmae設為預設值"ha-datacenter"
Datacenter dc = (Datacenter) new InventoryNavigator(rootFolder) .searchManagedEntity("Datacenter", dcname);設定DataCenter 取得Datacenter 類型的object
host = (HostSystem) new InventoryNavigator(dc).searchManagedEntity( "HostSystem", hostname);由輸入的HOST Name取得HostSystem類型的Object
vm = (VirtualMachine) new InventoryNavigator(si.getRootFolder()) .searchManagedEntity("VirtualMachine", VMname);取得VM,之後的動作皆對於vm這個object進行
Task task = vm.powerOnVM_Task(null);
建立一個power on的Task
task.waitForTask();將task放入待執行的list中等待執行
if (task.getTaskInfo().getState() == TaskInfoState.success) { System.out.println("Virtual Machine PowerOn successfully.");確認VM是否有成功Power on
Related Posts :
- Back to Home »
- [小PのIT筆記][VMware]vim »
- [VMware] 使用JAVA撰寫vim --VM Power on