在vmware的PowerCLI和微软的scvmm的PowerShell中可以通过Get-VM命令来获取虚拟机的信息。下面我们用代码的方式去实现,代码相关类方法就是对命令的封装。
vmware api的调用方式:
#region vm status
public VirtualMachineOperationEnum GetVMStatus(string vmName)
{
ManagedObjectReference vmMor = util.getServiceUtil().getEntityByName("VirtualMachine", vmName);
VirtualMachineRuntimeInfo info = (VirtualMachineRuntimeInfo)util.getServiceUtil().GetDynamicProperty(vmMor, "runtime");
switch (info.powerState)
{
case VirtualMachinePowerState.poweredOn:
return VirtualMachineOperationEnum.On;
case VirtualMachinePowerState.poweredOff:
return VirtualMachineOperationEnum.Off;
case VirtualMachinePowerState.suspended:
return VirtualMachineOperationEnum.Suspend;
default:
return VirtualMachineOperationEnum.Fault;
}
}
#endregion
微软api的调用方式:
#region vm status
public VirtualMachineOperationEnum GetVMStatus(string vmName)
{
try
{
switch (client.GetVirtualMachineByName(vmName).Status)
{
case DataContracts.VMComputerSystemStateInfo.Running:
return VirtualMachineOperationEnum.On;
case DataContracts.VMComputerSystemStateInfo.Paused:
return VirtualMachineOperationEnum.Paused;
case DataContracts.VMComputerSystemStateInfo.PowerOff:
return VirtualMachineOperationEnum.Off;
default:
return VirtualMachineOperationEnum.Fault;
}
}
catch { return VirtualMachineOperationEnum.Fault; }
}
#endregion