Correct shutdown of VMWare ESXi hypervisor at critical battery level of APC UPS

In the open spaces there are many articles on how to set up PowerChute Business Edition, and how to connect to VMWare from PowerShell, but somehow all this was not found in one place, with a description of the subtle points. And they are.



1. Introduction



Despite the fact that we have something to do with energy, problems with electricity sometimes arise. This is where the UPS comes into play, but its batteries, alas, are not durable. What to do? Turn off!



As long as all the servers were physical, things were going well, with the PowerChute Business Edition helping us out. Free, for 5 servers, which was enough. The agent, server and console were installed on one machine. When the end approached, the agent simply executed a batch file, in which shutdown.exe / s / m was sent to neighboring servers, and then extinguished its OS. Everybody is alive.

Then came the time of virtual machines.



2. Background and reflections



So what do we have? Nothing at all - one physical server running Windows Server 2008 R2 and one hypervisor with several virtual machines, including Windows Server 2019, and Windows Server 2003, and CentOS. And another UPS - APC Smart-UPS.



We heard about NUT, but we didn't get our hands on it yet, we used only what was at hand, namely PowerChute Business Edition.



The hypervisor is able to shutdown its virtual machines by itself, it remains only to tell it that it's time. There is such a useful thing VMWare.PowerCLI, this extension for Windows Powershell, just allows you to connect to the hypervisor and tell it everything you need. There are also many articles about PowerCLI settings in the open spaces.



3. Process



The UPS was physically connected to the com-port of the server 2008, since it was there. Although this is not essential - it was possible to connect through the interface converter (MOXA) to any virtual Windows server. Further, all actions are performed on the machine to which the UPS is connected - Windows Server 2008, unless explicitly stated otherwise. The PowerChute Business Edition agent was installed on it. Here is the first subtle point: the agent service must be started not from the system, but from the user, otherwise the agent will not be able to execute the cmd file.



Then we installed .Net Framework 4.7. A reboot is required here , even if the framework does not explicitly ask for it after installation, otherwise it will not go further. After that, updates may still come, you also need to install it.



Next, we installed PowerShell 5.1.A reboot is also required , even if not asked.

Next, install PowerCLI 11.5. Quite a fresh version, from this and the previous requirements. It is possible via the Internet, there are many articles about this, but we already downloaded it, so we just copied all the files into the Modules folder.



Checked:



Get-Module -ListAvailable


Ok, we see, installed:



Import-Module VMWare.PowerCLI


Yes, the Powershell console is of course launched from the Administrator.



Powershell settings.



  • Allow execution of any scripts:


Set-ExecutionPolicy Unrestricted


  • Or allow only ignore script certificates:


Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 


  • Allow PowerCLI to connect to servers with invalid (expired) certificates:


Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false


  • Suppress the output of the PowerCLI message about joining the exchange program, otherwise there will be a lot of unnecessary things in the log:


Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false


  • Save user credentials for logging into the VMWare host, so as not to explicitly expose them in the script:


New-VICredentialStoreItem -Host address -User user -Password 'password'


Checking will show who we have saved:



Get-VICredentialStoreItem


You can also check the connection: Connect-VIServer address.



The script itself, well, for example: connected, extinguished, disconnected just in case, options are possible:




    Connect-VIserver -Server $vmhost 
    Stop-VMHost $vmhost -force -Confirm:$false 
    Disconnect-VIserver $vmhost -Confirm:$false


4. Default.cmd



The same batch file that the APC agent launches. Lies in “C: \ Program Files [(x86)] \ APC \ PowerChute Business Edition \ agent \ cmdfiles”, and inside:



“C: \ Windows \ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe” -File “C : \ ... \ shutdown_hosts.ps1 "

It seems that everything was set up and checked, even started cmd - it works correctly, turns it off.



We run the command file check from the APC console (there is a Test button) - it does not work.



Here it is, that awkward moment when all the work done did not lead to anything.



5. Catharsis



We look at the task manager, we see - cmd flashed, powershell flashed. We take a closer look - cmd * 32 and, accordingly, powershell * 32. We understand that the APC agent service is 32-bit, which means that it starts the corresponding console.



Launch powershell x86 as administrator, install and configure PowerCLI from step 3 again,



and change the line for calling powershell:



"C:\Windows\<b>SysWOW64</b>\WindowsPowerShell\v1.0\powershell.exe…


6. Happy end!




All Articles