none
Command to Shutdown VM's

    Question

  • Hey guys,

    Hopefully a simple one here.....

    Looking to write a script to shutdown my VM's, easy one out there ?
    Setup:
    1 UPS + 1 Phyiscal Windows 2012R2 Server = 6 VM Servers.

    When my UPS hits a trigger point of low battery, it runs a file on the Win2012 (.BAT file)....
    I need that .BAT file to call a Script that starts the process to power down my VM's..  Allowing for say 9 minutes to safely power down these servers (DC, SQL, Exch), the final command would be for the Phyiscal Host Server to shutdown.

    As much as i know the Command is:   shutdown -s -t xx (time) for the Phyiscal server, how do i go about calling for the shutdown of the VM's ?

    Cheers

    Thursday, April 20, 2017 4:00 PM

All replies

  • When I searched the script repository it returned with these ones that might work for you.

    https://gallery.technet.microsoft.com/scriptcenter/site/search?query=shutdown%20vm&undefined=undefined&ac=3

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, April 20, 2017 4:09 PM
  • You can use PowerShell to shutdown your Virtual Machines. The cmdlet is Stop-VM

    MCSE, MCSA, MS, MCP, MCTS, System Engineer

    Thursday, April 20, 2017 4:36 PM
  • Hi,

    you can set up Automatic Start Action and Automatic Stop Action for any VM.

    When your physical host shuts down it will perform Stop Actions for VMs before shutting down. And when your physical host starts - it will perform Start Actions after system startup.

    EDIT: By default Virtual MAchines are set to Save the virtual machine state when Hypervisor shuts down.

    Radek


    Friday, April 21, 2017 6:05 AM
  • Funny you should say that....
    As i am in Test phase for my new UPS, and looking at all options of Graceful shutdown, i changed the settings on the VM's to say "Shutdown when the Host shutsdown", intergration services are enabled, etc.... (all services)

    However, the Win2012 Host starts the shutdown as normal, but the VM's just crash out, like pulling the plug style!  They dont do a shutdown followed by the server shutting down, and yes, Win2012 is upto-date!
    Tested this setup 4 times so far, so will keep going!

    And yes, SAVE state is by default and how it currently is in my Live environment, but as I have a DC, SQL and EXCH as VM's, they ideally need to be shutdown rather than 'saved'  :) :)
    Friday, April 21, 2017 8:40 AM
  • Hey all,

    I've got my Powershell Script now to Shutdown all VM's

      ((((  [cmdletbinding()]
    Param($vmhost = 'HYPER-V-TEST')
    $runningVM = Get-VM -ComputerName $vmhost| where state -eq 'running'
    foreach ($cn in $runningVM)
    {Stop-VM $cn.name -asjob}        ))))

    Questio now is:
    How do insert that into a .BAT  file ?

    The Shutdown Command i want is:

    Host executes a .BAT file which issues the command  =    Shutdown -s -t 600  (Server shutdown in 10 minutes)
    Then, the PowerShell Script runs to shut down my VM's.      (Average shutdown time for the VM's is from 15 seconds to my EXCH server taking 4 minutes).

    Thats it......   Any ideas ?

    Friday, April 21, 2017 10:28 AM
  • Think i got it :)

    I add this line into my  .BAT  file
    PowerShell.exe -noexit -Command "& '%~dpn0.ps1'"

    and it calls the .PS1 file which has the same name as my .BAT file.... and runs the script

    (without the -noexit, the script would'nt run unless i was in Pshell ISE).....
    Cool cool :)

    Friday, April 21, 2017 11:10 AM
  • Are you sure that Stop-VM will not crash your VMs the same way as if you shut down the host?

    I think you should investigate why your VMs are crashing out on "Shutdown when the Host shutsdown"

    because they should not crash...

    Radek

    Friday, April 21, 2017 11:12 AM
  • In my Powershell script, the  Stop-VM     part starts the Shutdown Process (graceful shutdown)..
    Works a treat :)

    As for the command in Hyper-V , that says "shutdown VM when it detects the OS is shutting down" , these crash out, as it appears the Host shuts down as normal, and doesnt wait for the VM to complete a shutdown....  Maybe a flaw with Windows ???

    Friday, April 21, 2017 11:32 AM
  • "these crash out, as it appears the Host shuts down as normal, and doesnt wait for the VM to complete a shutdown....  Maybe a flaw with Windows ???"

    Do all of them 'crash' or do just some of them?  This setting works fine if you have a very small number of VMs that do not take long to shut down.  The Windows host shutdown process has a timer.  When that timer expires, it will stops any graceful VM shutdowns and completes the shutdown of the host.  If you have too many VMs, or the VMs themselves take too long to gracefully shut down, that setting is not helpful.  It is better to use the script method you are developing.


    tim

    Friday, April 21, 2017 12:56 PM
  • Hiya

    This is only a test rig i am using at the moment, but the VM's are copies of the Live VM's (none networked! :D )
    I have x1  Win2008 r2 as a DC, x1  Win2008 r2 as an EXCH 2007 server, and x1  Win7 machine....
    If i shutdown the DC and Win7 VM, they are down within 12 seconds.....
    The Exch server runs between 6 to 9 minutes to power off!   (Host machine is Win2012 r2)

    Using the Hyper-V method of shutdown, when i reboot the VM's, the DC machine reports an unexpected shutdown, hence me thinking its a flaw within windows ?

    Script works ok, so all good on that note!:)

    Friday, April 21, 2017 1:18 PM
  • "Using the Hyper-V method of shutdown, when i reboot the VM's, the DC machine reports an unexpected shutdown, hence me thinking its a flaw within windows ?"

    Most likely because Exchange is involved.  Exchange is heavily dependent upon Active Directory, so AD might not be able to shut down while Exchange is actively trying to shut down.  And if Exchange is taking 6-9 minutes, that's too long.  I think Windows will wait about 5 minutes.  I know the automatic shutdown works for small environments where all the VMs can shutdown in 5 minutes, but I generally do not use that capability because just about anything can mess it up.  Stick with your script.


    tim

    Friday, April 21, 2017 1:47 PM