5 thoughts on “Use PowerCli to shutdown VM’s and Hosts when running on battery.

  1. Hi mwpreston !
    This is the best script I have found to perform this (one would think) very common requirement.
    In my case I will be having a local WinXP VMGuest shutdown the ESXi 4.1 Host it is running on. Whilst we have a VCS that is in our Data Centre – not out on the sites – so if the power goes off I have to assume it is likely the comms links will be affected as well…
    Whilst I happy with batch and bash scripts, the Powershell / powercli is all new to me so forgive my ignorance….
    So I have questions:
    1. What happens if a guest hangs on shutdown (e.g. stays at Windows power off screen). Some scripts do a second run through and force off these machines (whether this is required is a different matter).
    2. Is your method of triggering the Host shutdown the equivalent of using the -force parameter ?
    3. How are you making the PowerCLI connections to the vcs/hosts  ? I am trying to figure out a way to connect without having credentials in clear text in a file.
    Hope to hear from you soon but I'll keep digging in the meantime….
     
    Thanks
    Ian
     
     

    1. Hi Ian, great questions and I’ve updated the script to facilitate some of the items that you pointed out.

      I’m running under an account that has administrative priveledges on the vCenter Server, also, the connections are going through the vCenter Server as well…

  2. Thanks for the script.  This provides a complete solution for shutting down / suspending VM’s, putting hosts in maintenence mode, and then shutting down the hosts.  Very much appreciated!  I especially like the ability to bypass certain VM’s and leave them running until the end.I’ve made some modifications to allow the user to enter the parameter for the vCenter server as part of calling the script, or for the script to prompt the user for the name of the vCenter to which the script should establish a connection, and then to actually establish that connection.My only problem is with the function of putting the host in maintenence mode.  When done through the GUI it prompts whether to vMotion suspended and powered off VM’s to another host.  Since this script is intended to shut down the infrastructure I don’t want to vMotion any of my VMs when I enter maintenence mode.  Is there an argument that can be included in the command “Set-VMhost -VMhost $currentesxHost -State Maintenance” to prevent any of the VM’s from being vMotioned?

    I might play with the ability to have the script query the names of the hosts and then shut them down without having to include the host names in the script itself, or potentially allow for user input of host names.

    Thanks!

    1. Hi Thomas, thanks for the comment.
      Yes, I had the ability to bypass certain VMs just incase this script was actually running a VM itself, or if vCenter was virtualized. I had put the hosts in maintenance mode due to the fact that when I was doing some production level testing with this I found that HA was responding sometimes if VMs were not quite registered as being powered off. This led to VMs being restarted again on remaining hosts, then the script would move to the next host and do the same thing until either admission control went crazy or until everything was almost running on one host. Feel free to modify at will, if you get any great ideas post them back and we can update it. Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *