VMware Cloud Community
fborges555
Enthusiast
Enthusiast
Jump to solution

ESXI Maintenance mode

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
fborges555
Enthusiast
Enthusiast
Jump to solution

L.

A timeout as start-sleep or wait-process?

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
fborges555
Enthusiast
Enthusiast
Jump to solution

L.

as always thanksssssss!!!

Reply
0 Kudos