C# 使用PowerCLI.net C重新启动虚拟机#

C# 使用PowerCLI.net C重新启动虚拟机#,c#,.net,model-view-controller,vmware,powercli,C#,.net,Model View Controller,Vmware,Powercli,我一直在和这个人碰壁。目标是利用PowerCLI的API和C#重新启动VM。这将是一个MVC框架。用户登录并在登录页上获得要重新启动的服务器列表。选择服务器,并重新启动其中一个或多个服务器(复选框) 我的问题是理解PowerCLI,以及我需要什么。目前,我引用了VMware.Vim,它可以让我连接、获取服务器列表。但是,我不确定如何通过API重新启动服务器。下面是我安装的库。我不确定除了VMware.Vim之外,我还需要什么来实现这一点 我目前正在使用这段youtube视频作为视频。但是,任何

我一直在和这个人碰壁。目标是利用PowerCLI的API和C#重新启动VM。这将是一个MVC框架。用户登录并在登录页上获得要重新启动的服务器列表。选择服务器,并重新启动其中一个或多个服务器(复选框)

我的问题是理解PowerCLI,以及我需要什么。目前,我引用了VMware.Vim,它可以让我连接、获取服务器列表。但是,我不确定如何通过API重新启动服务器。下面是我安装的库。我不确定除了VMware.Vim之外,我还需要什么来实现这一点

我目前正在使用这段youtube视频作为视频。但是,任何文档、代码示例或任何指导都将不胜感激。这有点超出我的水平,我对PowerCLI完全不熟悉

非常感谢您抽出时间


PowerCLINooby

这是我在.net中用于重新启动虚拟机的代码。您需要的唯一库是
VMware.Vim

  • 首先,获取连接到vcenter的客户端
  • 获取虚拟机
  • 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");
        }
    }