4 Replies Latest reply on Mar 28, 2020 12:42 PM by fborges555

    ESXI Maintenance  mode

    fborges555 Enthusiast

      Hi

       

      I would like to put an esxi host in maintenance mode, but also I would like to know when the ESXi is on maintenance mode, then reboot the esxi once in maintenance mode, and check when it comes back up to exit maintenance mode. Hope I make sense

       

      connect-viserver

      $esxihost="esxi"

      get-vmhost -name $esxihost | set-Vmhost -state Maintenance

      "I would like to check the status until esxi is on maintenance mode"

      Restart-VMhost $esxihost

      " check when comes up into maintenance mode"

      get-vmhost-Name $esxihost | set-VMhost -state connected

       

      How am I doing??

       

      Thanks

        • 1. Re: ESXI Maintenance  mode
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          A While-loop would be one way to check for a state change.

          Something like this for example.

          One word of warning, if an ESXi has issues going into maintenance mode or coming out of maintenance mode, you could be stuck in an endless loop.

          This could be avoided by implementing a timeout.

           

           

          Connect-VIServer

          $esxihost = "esxi"

          $esx = Get-VMHost -Name $esxihost | Set-VMHost -State Maintenance

          while($esx.ConnectionState -ne 'Maintenance'){

              sleep 5

              $esx = Get-VMHost -Name $esx.Name

          }

          Restart-VMHost $esxihost

          while($esx.ConnectionState -eq 'Maintenance'){

              sleep 5

              $esx = Get-VMHost -Name $esx.Name

          }

          Get-VMHostvmhost-Name $esxihost | Set-VMHost -State connected

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: ESXI Maintenance  mode
            fborges555 Enthusiast

            L.

             

            A timeout as start-sleep or wait-process?

             

            Thanks

            • 3. Re: ESXI Maintenance  mode
              LucD Guru
              Community WarriorsvExpertUser Moderators

              No, a coded timeout.

              Something like this for example

               

              $timeout = 300

              Connect-VIServer

              $esxihost = "esxi"

              $esx = Get-VMHost -Name $esxihost | Set-VMHost -State Maintenance

              $start = Get-Date

              $span = New-TimeSpan -Start $start -End (Get-Date)

              while($esx.ConnectionState -ne 'Maintenance' -and $span.TotalSeconds -le $timeout){

                  sleep 5

                  $esx = Get-VMHost -Name $esx.Name

                  $span = New-TimeSpan -Start $start -End (Get-Date)

              }

              if($span.TotalSeconds -gt $timeout){

                  Write-Host "Host didn't go into maintenance in time"

                  return

              }

              Restart-VMHost $esxihost

              $start = Get-Date

              $span = New-TimeSpan -Start $start -End (Get-Date)

              while($esx.ConnectionState -eq 'Maintenance' -and $span.TotalSeconds -le $timeout){

                  sleep 5

                  $esx = Get-VMHost -Name $esx.Name

                  $span = New-TimeSpan -Start $start -End (Get-Date)

              }

              if($span.TotalSeconds -gt $timeout){

                  Write-Host "Host didn't start in time"

                  return

              }

              }

              Get-VMHostvmhost-Name $esxihost | Set-VMHost -State connected

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: ESXI Maintenance  mode
                fborges555 Enthusiast

                L.

                 

                as always thanksssssss!!!