PowerCLI

 View Only
Expand all | Collapse all

Host shutdown via ssh esxi 7.0

Scott Vessey

Scott VesseyJan 26, 2021 04:38 PM

  • 1.  Host shutdown via ssh esxi 7.0

    Posted Jan 22, 2021 10:27 PM

    Hi everyone,

    I am trying to create a shutdown script issued via ssh that will first shutdown all my vms as per the policies I have defined in the autostart/shutdown settings, and then power off the host.

    After researching I found out that up until esxi 6.7 this could be achieved using /sbin/shutdown.sh && /sbin/poweroff .

    However, shutdown.sh in esxi 7.0 is deprecated. This is what's in the script:

    #The usage of shutdown.sh is deprecated!
    #We keep the file only for legacy reasons. Do not add any new code.

    So I tried to use host_shutdown.sh with only 2 test vm's running (a win10 machine and a debian machine) to see if they would shutdown gracefully before the host.

    Results were mixed. The debian machine shutdown as it should, and stayed shut, and the win10 machine shutdown and reboot right after! The host did power off while the second machine was booting... I confirmed from the logs that the win10 machine started the reboot sequence 10seconds after it was shutdown, which is equal to the stop delay I had configured. The win10 was the last in order to shutdown.

    Because this is a production server, I can't do many tests..Does anyone have experience with host_shutdown.sh in esxi 7.0? Is it the designated method for remotely shutting down vm's and then the host?

    I haven't been able to find any relevant information on the web.

    S.

     



  • 2.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 02:43 PM

    You could also use PowerCLI for this...

    #Connect to ESXi host
    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
    $VMHost = "myhostname"
    $Credentials = Get-Credential -Message "Enter valid credentials..." -UserName root
    Connect-VIServer -Server $VMHost -Credential @Credentials | Out-Null

    #Stop VMs
    Get-VM | Stop-VM

    #Shutdown host
    Stop-VMHost -VMHost $VMHost -Force

     



  • 3.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 03:52 PM

    Hi,

    Thanks for the answer...from a little searching I did, issuing

    Stop-VM powers off the virtual machine, it does not cleanly shut it down using vmware tools.

    For that I think it should be either Stop-VMGuest or Shutdown-VM...correct?

    And on top of that when issuing such a command there should be a confirmation step you need to go through?!

    However this does not take account that I need the vm's to shutdown in the sequence that is configured in the Startup Sequence.

    Will this script wait for all the vm's to shut down properly before powering off the host?

     



  • 4.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 04:18 PM

    if you have vmware-tools installed you can stop the vm with "Shutdown-VMGuest (Get-VM $vm) -Confirm:$false "

    then you can wait for the vm powered down with checking the status: "$status = (get-VM $vm).PowerState"

    if $status -ne "PoweredOff" then loop until status reached

     



  • 5.  RE: Host shutdown via ssh esxi 7.0

    Posted Feb 16, 2024 10:07 AM
      |   view attached

    Hi there,

    i am using ESXI 7.0.3

    i would like to shutdown the host. But it does not work.

    Can somebody help me to solve this issue?

     

    Best Regards

    JC

     



  • 6.  RE: Host shutdown via ssh esxi 7.0

    Posted Feb 16, 2024 11:04 AM

    i think the error message is clear enough ?

    i assume you are using the free esxi license ? with this license the api is readonly, that means you cannot do anything with powercli

    the only way is shutdown via ssh



  • 7.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 04:38 PM

     

    Moderator: Moved to PowerCLI Discussions



  • 8.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 05:10 PM

    Since you mention Startup Sequence for the VMs, I gather that the ESXi node is not part of a cluster with HA enabled?
    If yes, I assume you intend the shutdown of the VMs to happen in the reverse order of the order configured in the Startup Sequence.
    That was what shutdown.sh was doing in fact.
    Correct?

     



  • 9.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 05:56 PM

    Correct, it's a single host, no HA. About 15 VM's running on it.

    And again correct, I expect-wish that the shutdown sequence will be the reverse to the configured Startup Sequence.

    Does  host_shutdown.sh have the same functionality with shutdown.sh?

    I haven't used PowerCLI before. Good thing is you don't have to have ssh enabled on the host for it to function, correct?

     

     



  • 10.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:04 PM

    That is correct, no SSH required for doing this with PowerCLI.

    Let me try and get something together.



  • 11.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:22 PM

    I tried what jburen suggested (up until Get-VM) and it works fine.

    berndweyand I cannot do tests right now to check if the shutdown  sequence will work though...I might have to setup a test host on a spare machine...



  • 12.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:48 PM

    I know my suggestion was very quick and dirty. The code given by  is much nicer. If you don't want a command to actually do something you can also add -WhatIf to that command (if supported). Or turn that code into comment by adding # in front of that code. But that's general PowerShell syntax...



  • 13.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:21 PM

    do you use the free license or standard/enterprise license ? with the free license you have no chance to do it with powercli because the api is readonly

     


  • 14.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:23 PM

    Standard license, as I mentioned I was able to connect through PowerCLI



  • 15.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:31 PM

    Try something like the following.
    You should be connected to the ESXi node (Connect-VIServer).

     

     

    $esxName = 'your-esxi-node'
    
    $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 'poweredon'}.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"
    }

     

     



  • 16.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:46 PM

    LucD, I will have to wait for the weekend to try this on my production server.

    Would rather try it on a test machine first. For the test host machine, I guess I will be able to install esxi 7.0 as an evaluation version and run the powercli without having to install the license?! 

    Can I put all this in a .ps1 script together with the 

    #Connect to ESXi host
    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
    $VMHost = "myhostname"
    $Credentials = Get-Credential -Message "Enter valid credentials..." -UserName root
    Connect-VIServer -Server $VMHost -Credential @Credentials | Out-Null

    part so it can execute and add credentials as well?

    I want this to run from Eaton IPP when the UPS issues a shutdown command (and it has to run on one of the VM's).

     

     



  • 17.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:49 PM

    Yes, afaik nothing has changed in the Startup Sequence between 6.5 and 7.
    And yes, with an evaluation license you should be able to test that.
    And yes, you can include those lines before my code.



  • 18.  RE: Host shutdown via ssh esxi 7.0

    Posted Jan 26, 2021 06:55 PM

    I made a small script to test connecting with credentials and it works fine...

    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
    Connect-VIServer -Server myhost -User root -password mypassword | Out-Null
    Get-VM

     

    I am starting to like powerCLI!!

    Thank you all, I will report back after I do some testing!