PowerCLI

 View Only
Expand all | Collapse all

Script to shutdown or suspend VM's and then turn off Host

  • 1.  Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 01:42 PM

    Hi, I have been looking for a script that would do the following:

    - suspend all VM's residing on the host according to VM Startup and Shutdown predefined on the host

    then

    - turn the host off gracefully

    I really need something right away, if someone helps me out I don't mind sending out $20 gift card. (for working script).



  • 2.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 02:50 PM


  • 3.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 03:26 PM

    LucD, thank you. I found this script which works to the extent that it shuts down all VM's but not the host. Does your script shutdown ESXi at the end? Is there also a way to make the script to suspend those machines vs. shutdown? What I really want is 

    1. shutdown or suspend the virtual machines gracefully, no hard shutdowns

    2. shutdown ESXi host

    3. I need to be able to run this script from one of the machines on that ESXi host, so it needs to go down as the last one and then issue shutdown to host.

    Script I found:

    # list of hosts to process
    $listofhosts = "IP or NAME OF ESXi"

    #loop through each host
    Foreach ($esxhost in $listofhosts)
    {
    $currentesxhost = get-vmhost $esxhost
    Write-Host "Processing $currentesxhost"

    #loop through each vm on host
    Foreach ($VM in ($currentesxhost | Get-VM | where { $_.PowerState -eq "PoweredOn" }))
    {
    Write-Host "===================================================================="
    Write-Host "Processing $vm"

    # if this is a vm that is supposed to go down with the ship.
    if ($vmstoleave -contains $vm)
    {
    Write-Host "I am $vm - I will go down with the ship"
    }
    else
    {
    Write-Host "Checking VMware Tools...."
    $vminfo = get-view -Id $vm.ID
    # If we have VMware tools installed
    if ($vminfo.config.Tools.ToolsVersion -eq 0)
    {
    Write-Host "$vm doesn't have vmware tools installed, hard power this one"
    # Hard Power Off
    Stop-VM $vm -confirm:$false

    }
    else
    {
    write-host "I will attempt to shutdown $vm"
    # Power off gracefully
    $vmshutdown = $vm | shutdown-VMGuest -Confirm:$false
    }
    }
    Write-Host "===================================================================="
    }
    Write-Host "Initiating host shutdown in 40 seconds"
    sleep 40
    #look for other vm's still powered on.

    Foreach ($VM in ($currentesxhost | Get-VM | where { $_.PowerState -eq "PoweredOn" }))
    {
    Write-Host "===================================================================="
    Write-Host "Processing $vm"
    Stop-VM $vm -confirm:$false
    Write-Host "===================================================================="
    }

    #Shut down the host
    Sleep 20

    Set-VMhost -VMhost $currentesxHost -State Maintenance
    Sleep 15

    $currentesxhost | Foreach {Get-View $_.ID} | Foreach {$_.ShutdownHost_Task($TRUE)}

    }
    Write-Host "Shutdown Complete"



  • 4.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 04:36 PM

    Yes, that is the Set-VMHost and Stop-VMHost doing.

    But where does the script you found handle your "VM Startup and Shutdown predefined on the host" requirement?



  • 5.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 04:40 PM

    LucD - script I found is not doing that, was just testing it and I liked it only because it had a line to leave one of the VM's on which was driving this script to shut down at the end and go down with host.

    Can you modify your script to the extent to put:

    - $vmstoleave = "NameOfVM"

    * Also I've run your script from my computer, it shutdown all of VM's but did NOT shut down the host.

    and only shut it down as the last VM because it will run this script and then finally shut down the ESXi server? Would it be OK if we chat via some type of IM?



  • 6.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 05:14 PM

    Do I understand correctly you only have 1 ESXi node?
    Then the script can't shut down the ESXi node which runs the VM on which the script is running..
    Sorry, but I really can't fix impossible 

    No, I don't do chats.
    Can you imagine how many hours a day I would be busy with chat sessions.
    That is simply not realistic I'm afraid.



  • 7.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 05:24 PM

    Yes you are correct, but we have several single ESXi servers in our branches.

    We have about 70 branches, each branch has 1 ESXi server.

    So your script cannot be modified to gracefully shut down the one ESXi server after all VM's shutdown?

    Sorry about the IM question I completely understand.



  • 8.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 05:27 PM

    Not really.
    The only option is to run that script on another machine (which is not shut down as part of the ESXi shutdown).



  • 9.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 05:45 PM

    LucD that is what I did for testing as well. I've run your script from my Windows10 machine from powercli. It turn off all Vm's but did not shut down the host.

    OSFullName : Microsoft Windows Server 2016 (64-bit)
    IPAddress : {IP}

    State : ShuttingDown

    I got this for all 4VM's but host did not turn off.

    at the end i got this: WARNING: One or more VMs did not stop

    but all of them stopped and shutdown.



  • 10.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 05:52 PM

    The script only tries a graceful shutdown, it will not force a stop (with Stop-VM).
    If the VMware Tools are not installed on all VMs, then the graceful shutdown will not work for all VMs.
    The warning is from the script when it discovers that 1 or more VMs are still powered on.

     



  • 11.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 06:38 PM

    As I've mentioned before ALL VM's shutdown without issue. I am only speaking about the host (ESXi server) it does NOT shut down after all VM's are turned off.



  • 12.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 07:15 PM

    When the script is running on one of these VMs, the shutdown of the VM interrupts the script.



  • 13.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 07:17 PM

    I am running the script from my computer not one of the VM's residing on that host.

     



  • 14.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 07:23 PM

    Just noticed one line was incorrect.
    Try with this change

    if((Get-VM -Location $esx).where{$_.PowerState -eq 'poweredoff'}.Count -eq 0){


  • 15.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 07:45 PM

    Posting the whole script once more including the output. I am logged onto esxi host directly, i see machines are nicely being shutdown but host remains ON.

    $esxName = '10.11.155.121'

    $esx = Get-VMHost -Name $esxName
    $esx.ExtensionData.Config.AutoStart.PowerInfo |
    ForEach-Object -Process {
    New-Object -TypeName PSObject -Property @{
    VM = (Get-View -Id $_.Key -Property Name).Name
    Order = $_.StartOrder
    }
    } | Sort-Object -Property Order,Name -Descending |
    ForEach-Object -Process {
    $vm = Get-VM -Name $_.VM
    Shutdown-VMGuest -VM $vm -Confirm:$false
    while($vm.ExtensionData.Runtime.PowerState -eq 'poweredOn'){
    sleep 2
    $vm.ExtensionData.UpdateViewData('Runtime.PowerState')
    }
    }

    if((Get-VM -Location $esx).where{$_.PowerState -eq 'poweredoff'}.Count -eq 0){
    Set-VMHost -VMHost $esx -State Maintenance
    while(-not $esx.ExtensionData.Runtime.inMaintenanceMode){
    sleep 2
    $esx.ExtensionData.UpdateViewData('Runtime.inMaintenanceMode')
    }
    Stop-VMHost -VMHost $esx -Confirm:$false
    }
    else{
    Write-Warning "One or more VMs did not stop"
    }

     

    When script is run:

    Name Port User
    ---- ---- ----
    10.11.155.121 443 root

    OSFullName : Microsoft Windows Server 2016 (64-bit)
    IPAddress : {10.11.110.240}
    State : ShuttingDown
    Disks : {Capacity:2144333824, FreeSpace:1983119360, Path:D:\, Capacity:63897071616, FreeSpace:19686383616,
    Path:C:\}
    HostName : BR110DC
    Nics : {Network adapter 1:10.11.110.x}
    ScreenDimensions : {Width=1280, Height=800}
    VmId : VirtualMachine-2
    VM : BR110DC
    VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-2/
    VmName : BR110DC
    Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-2/VMGuest=/
    GuestId : windows9Server64Guest
    ToolsVersion : 10.1.15
    ExtensionData : VMware.Vim.GuestInfo
    Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
    GuestFamily : windowsGuest


    OSFullName : Microsoft Windows Server 2016 (64-bit)
    IPAddress : {10.11.155.133, fe80::d1a6:92a5:fa62:2999}
    State : ShuttingDown
    Disks : {Capacity:63898120192, FreeSpace:51471728640, Path:C:\}
    HostName : Test01
    Nics : {Network adapter 1:LAN}
    ScreenDimensions : {Width=1920, Height=853}
    VmId : VirtualMachine-5
    VM : Test01
    VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-5/
    VmName : Test01
    Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-5/VMGuest=/
    GuestId : windows9Server64Guest
    ToolsVersion : 10.1.15
    ExtensionData : VMware.Vim.GuestInfo
    Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
    GuestFamily : windowsGuest


    OSFullName : Microsoft Windows Server 2016 (64-bit)
    IPAddress : {10.11.110.100, 169.254.179.203}
    State : ShuttingDown
    Disks : {Capacity:2308838518784, FreeSpace:1053267234816, Path:D:\, Capacity:322119397376,
    FreeSpace:278769627136, Path:E:\, Capacity:53159653376, FreeSpace:13294333952, Path:C:\}
    HostName : BR110DFS.corp.kearnyfederalsavings.net
    Nics : {Network adapter 1:10.11.110.x}
    ScreenDimensions : {Width=1024, Height=768}
    VmId : VirtualMachine-1
    VM : BR110DFS
    VmUid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-1/
    VmName : BR110DFS
    Uid : /VIServer=root@10.11.155.121:443/VirtualMachine=VirtualMachine-1/VMGuest=/
    GuestId : windows9Server64Guest
    ToolsVersion : 10.1.15
    ExtensionData : VMware.Vim.GuestInfo
    Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
    GuestFamily : windowsGuest

    WARNING: One or more VMs did not stop

    So after this warning nothing happens to the host, it remains on.



  • 16.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 07:49 PM

    My bad, that 'poweredoff' should of course be 'poweredon'



  • 17.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 08:25 PM

    That worked, thank you, is there a way to "suspend" them vs. power off and then power off the host?



  • 18.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 08:43 PM

    You can use Suspend-VM instead of Shutdown-VMGuest.
    Replace

     

    Shutdown-VMGuest -VM $vm -Confirm:$false
    

     

    with

     

    Suspend-VM -VM $vm -Confirm:$false

     



  • 19.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 08:54 PM

    BTW like I stated I can send $20 paypal or venmo, send me something i can sent to and you will get it.



  • 20.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 04, 2021 09:05 PM

    Thanks for the offer, but I do this as a community contribution.
    No reward required 



  • 21.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 05, 2021 04:09 PM

    LucD, I really like your script but issue with running this script from another computer/server is problematic for us. As I've stated we have 70 branches, each branch has one ESXi host. If we put this script and trigger it to run from our corporate office that would work, but in case of ISP issues etc we won't be able to issue that command.

    If we pre-define Startup/Shutdown in ESXi and only issue command like: 

    Get-VMHost -server "ServerName" | Stop-VMHost -Force -Confirm:$false

    Wouldn't ESXi first shut down gracefully VM's residing on it and then turn itself off? Bottom line we have to figure out how to run this command internally from within one of the VM's.

    APC PowerChute does it when you have it installed on ESXi, I assume that is all based on scripts....



  • 22.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 05, 2021 04:46 PM

    If these are indeed standalone ESXi nodes then you could indeed use the Automatic startup/shutdown feature.
    See also Edit Virtual Machine Startup and Shutdown Settings (vmware.com)

    You should probably use the RunAsync switch on the Stop-VMHost cmdlet.



  • 23.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 05, 2021 07:03 PM

    can you send me the whole command? Runasync?



  • 24.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Feb 05, 2021 07:07 PM

    You could do something like this

    Get-VMHost -Name MyEsx |
    Stop-VMHost -RunAsync -Confirm:$false


  • 25.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Jul 15, 2022 10:06 PM


    Would this work if the VM tools are out of date?

    Also the Linux machines that don’t have VM tools would just not be effected  from the script?

     

    TT



  • 26.  RE: Script to shutdown or suspend VM's and then turn off Host

    Posted Aug 16, 2022 07:22 AM

    Is there any final draft of the script I can try to run?
    BTW: is there option to turn off esxi before the turn off the VMs? Because I want one of the vm's will trigger the shutdown process.