我已经和这个墙碰壁了。目标是使用PowerCLI的API和C#重新启动VM。这将是一个MVC框架。用户在登录页面和登录页面上会获得要重新启动的服务器列表。选择服务器,然后重新启动其中一个或多个(复选框)。
我的问题是了解PowerCLI和我所需要的。当前,我引用了VMware.Vim,它可以允许我连接并获取服务器列表。但是,我不确定如何通过API重新启动服务器。以下是我安装的库。我不确定除VMware.Vim之外还需要其他什么来实现此目的。
https://code.vmware.com/web/tool/12.0.0/vmware-powercli
我目前正在将此youtube视频用作starting point。但是,将不胜感激任何文档,代码示例或任何指导。这有点超出我的水平,并且完全不熟悉PowerCLI。
非常感谢您的宝贵时间,
PowerCLINooby
这是我在.net中用来重新启动VM的代码。您唯一需要的库是VMware.Vim
。
public static VimClient ConnectToVCenter(string vCenterUrl, string username, string password)
{
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
VimClient client = new VimClientImpl();
try
{
client.Connect($"https://{vCenterUrl}/sdk");
}
catch (Exception ex)
{
throw new ApplicationException($"Unable to connect to [{vCenterUrl}]. Exception: [{ex.Message}]");
}
client.Login(username, password);
return client;
}
public static VirtualMachine GetVM(VimClient client, string guestVM, ManagedObjectReference parentMoRef = null)
{
// Lookup VM in vCenter.
NameValueCollection Filter = new NameValueCollection
{
{ "name", guestVM }
};
List<VirtualMachine> virtualMachines = GetEntities<VirtualMachine>(client, parentMoRef, Filter, null);
// Do some error checking to make sure your VM was found.
return virtualMachines.FirstOrDefault();
}
// Helper function that can be used to look up various objects like VirtualMachine, Datastore, etc.
public static List<T> GetEntities<T>(VimClient vimClient, ManagedObjectReference beginEntity, NameValueCollection filter, string[] properties)
{
return vimClient.FindEntityViews(typeof(T), beginEntity, filter, properties)?.Cast<T>().ToList();
}
// Use enum for power operations
public enum PowerOp
{
suspend,
reset,
powerOff,
gracefulOff,
powerOn,
powerOnWaitForTools,
gracefulReboot,
standby
}
public static void PowerOperation(VimClient client, VirtualMachine vm, PowerOp operation)
{
switch (operation)
{
case PowerOp.suspend:
if (vm.Runtime.PowerState != VirtualMachinePowerState.suspended)
client.WaitForTask(vm.SuspendVM_Task());
break;
case PowerOp.reset:
client.WaitForTask(vm.ResetVM_Task());
break;
case PowerOp.powerOff:
if (vm.Runtime.PowerState != VirtualMachinePowerState.poweredOff)
{
client.WaitForTask(vm.PowerOffVM_Task());
}
break;
case PowerOp.powerOn:
if (vm.Runtime.PowerState != VirtualMachinePowerState.poweredOn)
{
client.WaitForTask(vm.PowerOnVM_Task(vm.Runtime.Host));
}
break;
case PowerOp.powerOnWaitForTools:
if (vm.Runtime.PowerState != VirtualMachinePowerState.poweredOn)
{
client.WaitForTask(vm.PowerOnVM_Task(vm.Runtime.Host));
}
vm.WaitForTools(client);
break;
case PowerOp.gracefulReboot:
vm.RebootGuest();
Thread.Sleep(10000); // Wait at least 10 seconds to let the server start with reboot.
vm.WaitForTools(client);
Thread.Sleep(5000); // Just a precaution.
break;
case PowerOp.gracefulOff:
if (vm.Runtime.PowerState != VirtualMachinePowerState.poweredOff)
vm.ShutdownGuest();
vm.WaitForPowerState(client, VirtualMachinePowerState.poweredOff);
break;
case PowerOp.standby:
vm.StandbyGuest();
break;
default:
throw new ApplicationException($"Error: Unknown power operation requested");
}
}